源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

android闹铃简单实现

  • 时间:2022-12-16 03:15 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:android闹铃简单实现
1.闹铃activity
[url=http://schemas.android.com/apk/res/android]http://schemas.android.com/apk/res/android[/url]"     xmlns:tools="[url=http://schemas.android.com/tools]http://schemas.android.com/tools[/url]"     android:layout_width="match_parent"     android:layout_height="match_parent"     >     <Button android:id="@+id/setTime"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:text="设置时间"/>     <Button android:id="@+id/setRing"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_below="@+id/setTime"         android:text="设置铃声"/>     <Button android:id="@+id/setOver"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_below="@+id/setRing"         android:text="设置完成"/> </RelativeLayout>
3.广播接收闹铃信息:
[u]复制代码[/u] 代码如下:
package com.example.myalarm; import java.io.IOException; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.media.MediaPlayer; import android.net.Uri; import android.util.Log; import android.widget.Toast; public class AlarmBroadcastReceiver extends BroadcastReceiver {     Uri ringUri;     @Override     public void onReceive(Context context, Intent intent) {         String msg = intent.getStringExtra("msg");         Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();         MediaPlayer mp = new MediaPlayer();         String uri = intent.getStringExtra("ringURI");         if (uri!=null) {             ringUri = Uri.parse(uri);             Log.d("AlarmActivity", ringUri.toString());         }         try {             mp.setDataSource(context, ringUri);             mp.prepare();         } catch (IllegalArgumentException e) {             e.printStackTrace();         } catch (SecurityException e) {             e.printStackTrace();         } catch (IllegalStateException e) {             e.printStackTrace();         } catch (IOException e) {             e.printStackTrace();         }         mp.start();     } }
4.在manifest文件中注册广播
[u]复制代码[/u] 代码如下:
 <receiver android:name="com.example.myalarm.AlarmBroadcastReceiver"></receiver>
以上代码就是实现Android闹钟的全部代码了,希望大家能够喜欢。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部