Android 使用AlarmManager和NotificationManager来实现闹钟和通知栏
程序员文章站
2023-12-19 08:18:34
实现闹钟运行的效果如下:
通知栏的运行后效果图如下:
布局文件(activity_main.xml)
实现闹钟运行的效果如下:
通知栏的运行后效果图如下:
布局文件(activity_main.xml)
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" android:orientation="vertical" tools:context="com.example.g150825_android28.mainactivity"> <button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="设置闹钟(一次)" android:onclick="setalarmone" /> <button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="设置闹钟(周期)" android:onclick="setalarm" /> <button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="取消周期闹钟" android:onclick="cancelalarm" /> <button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="发送通知" android:onclick="send" /> </linearlayout>
activity_ring.xml
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_ring" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" tools:context="com.example.g150825_android28.ringactivity"> <button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="停止" android:onclick="stop" android:id="@+id/button" android:layout_alignparenttop="true" android:layout_alignparentleft="true" android:layout_alignparentstart="true" /> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="慈禧太后青霜来了,赶紧起床!" android:id="@+id/textview" android:textsize="30sp" android:layout_below="@+id/button" android:layout_centerhorizontal="true" android:layout_margintop="36dp" /> </relativelayout>
ringactivity
package com.example.g150825_android28; import android.media.mediaplayer; import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.view.view; public class ringactivity extends appcompatactivity { private mediaplayer mediaplayer; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_ring); mediaplayer = mediaplayer.create(this, r.raw.sqbm); mediaplayer.start(); } public void stop(view view){ mediaplayer.stop(); finish(); } }
myreceiver
package com.example.g150825_android28; import android.content.broadcastreceiver; import android.content.context; import android.content.intent; import android.widget.toast; public class myreceiver extends broadcastreceiver { public myreceiver() { } @override public void onreceive(context context, intent intent) { if("com.example.g150825_android28.ring".equals(intent.getaction())){ toast.maketext(context, "闹钟响了", toast.length_short).show(); //跳转到activity intent intent1=new intent(context,ringactivity.class); //设置标志位(flag) intent1.setflags(intent.flag_activity_new_task); context.startactivity(intent1); } } }
清单文件(androidmanifest.xml)
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.g150825_android28"> <application android:allowbackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsrtl="true" android:theme="@style/apptheme"> <activity android:name=".mainactivity"> <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> <receiver android:name=".myreceiver" android:enabled="true" android:exported="true"> <intent-filter> <action android:name="com.example.g150825_android28.ring" /> </intent-filter> </receiver> <activity android:name=".ringactivity" android:theme="@style/theme.appcompat.dialog" ></activity> </application> </manifest>
以上所述是小编给大家介绍的android 使用alarmmanager和notificationmanager来实现闹钟和通知栏,希望对大家有所帮助