Android 使用AlarmManager和NotificationManager來(lái)實(shí)現(xiàn)鬧鐘和通知欄
實(shí)現(xiàn)鬧鐘運(yùn)行的效果如下:
通知欄的運(yùn)行后效果圖如下:
布局文件(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="設(shè)置鬧鐘(一次)" android:onClick="setAlarmOne" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="設(shè)置鬧鐘(周期)" 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="發(fā)送通知" 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="慈禧太后青霜來(lái)了,趕緊起床!" 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(); //跳轉(zhuǎn)到Activity Intent intent1=new Intent(context,RingActivity.class); //設(shè)置標(biāo)志位(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來(lái)實(shí)現(xiàn)鬧鐘和通知欄,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- Android通過(guò)AlarmManager類實(shí)現(xiàn)簡(jiǎn)單鬧鐘功能
- Android編程使用AlarmManager設(shè)置鬧鐘的方法
- Android手機(jī)鬧鐘服務(wù)AlarmManagerk開(kāi)發(fā)案例
- 簡(jiǎn)單實(shí)現(xiàn)Android鬧鐘程序 附源碼
- Android編程實(shí)現(xiàn)鬧鐘的方法詳解
- Android實(shí)現(xiàn)簡(jiǎn)易鬧鐘功能
- 簡(jiǎn)單實(shí)現(xiàn)Android鬧鐘功能
- Android鬧鐘設(shè)置的解決方案
- Android鬧鐘機(jī)制實(shí)現(xiàn)定時(shí)任務(wù)功能
- Android使用AlarmManager設(shè)置鬧鐘功能
相關(guān)文章
Android videoview搶占焦點(diǎn)的處理方法
這篇文章主要為大家詳細(xì)介紹了Android videoview搶占焦點(diǎn)的處理方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06Android開(kāi)發(fā)中LayoutInflater用法詳解
這篇文章主要介紹了Android開(kāi)發(fā)中LayoutInflater用法,結(jié)合實(shí)例形式分析了LayoutInflater類的功能、作用、使用方法及相關(guān)注意事項(xiàng),需要的朋友可以參考下2016-08-08Android普通應(yīng)用升級(jí)為系統(tǒng)應(yīng)用并獲取系統(tǒng)權(quán)限的操作
這篇文章主要介紹了Android普通應(yīng)用升級(jí)為系統(tǒng)應(yīng)用并獲取系統(tǒng)權(quán)限的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-03-03Android實(shí)用的代碼片段 常用代碼總結(jié)
這篇文章主要介紹了Android實(shí)用的代碼片段 常用代碼總結(jié),需要的朋友可以參考下2014-09-09Android編程實(shí)現(xiàn)可滑動(dòng)的開(kāi)關(guān)效果(附demo源碼下載)
這篇文章主要介紹了Android編程實(shí)現(xiàn)可滑動(dòng)的開(kāi)關(guān)效果,涉及Android的布局與控件設(shè)置技巧,并附帶demo源碼供讀者下載參考,需要的朋友可以參考下2016-04-04Android ListView適配器(Adapter)優(yōu)化方法詳解
這篇文章主要介紹了Android ListView優(yōu)化方法詳解的相關(guān)資料,這里舉例說(shuō)明該如何對(duì)ListView 進(jìn)行優(yōu)化,具有一定的參考價(jià)值,需要的朋友可以參考下2016-11-11android BottomSheetDialog新控件解析實(shí)現(xiàn)知乎評(píng)論列表效果(實(shí)例代碼)
BottomSheetDialog是一個(gè)自定義的從底部滑入的對(duì)話框,這篇文章主要介紹了android BottomSheetDialog新控件解析實(shí)現(xiàn)知乎評(píng)論列表效果,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04Android開(kāi)發(fā)Jetpack組件Lifecycle原理篇
這一篇文章來(lái)介紹Android?Jetpack架構(gòu)組件的Lifecycle;?Lifecycle用于幫助開(kāi)發(fā)者管理Activity和Fragment?的生命周期,?由于Lifecycle是LiveData和ViewModel的基礎(chǔ);所以需要先學(xué)習(xí)它2022-08-08