Android 使用AlarmManager和NotificationManager來實(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="慈禧太后青霜來了,趕緊起床!"
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來實(shí)現(xiàn)鬧鐘和通知欄,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
- Android通過AlarmManager類實(shí)現(xiàn)簡單鬧鐘功能
- Android編程使用AlarmManager設(shè)置鬧鐘的方法
- Android手機(jī)鬧鐘服務(wù)AlarmManagerk開發(fā)案例
- 簡單實(shí)現(xiàn)Android鬧鐘程序 附源碼
- Android編程實(shí)現(xiàn)鬧鐘的方法詳解
- Android實(shí)現(xiàn)簡易鬧鐘功能
- 簡單實(shí)現(xiàn)Android鬧鐘功能
- Android鬧鐘設(shè)置的解決方案
- Android鬧鐘機(jī)制實(shí)現(xiàn)定時(shí)任務(wù)功能
- Android使用AlarmManager設(shè)置鬧鐘功能
相關(guān)文章
詳解Android 8.1.0 Service 中 彈出 Dialog的方法
這篇文章主要介紹了Android 8.1.0 Service 中怎么彈出 Dialog問題,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-10-10
Android程序開發(fā)之使用Design包實(shí)現(xiàn)QQ動(dòng)畫側(cè)滑效果和滑動(dòng)菜單導(dǎo)航
這篇文章主要介紹了Android程序開發(fā)之使用Design包實(shí)現(xiàn)QQ動(dòng)畫側(cè)滑效果和滑動(dòng)菜單導(dǎo)航的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-07-07
Android EditText實(shí)現(xiàn)分割輸入內(nèi)容
這篇文章主要為大家詳細(xì)介紹了Android EditText實(shí)現(xiàn)分割輸入內(nèi)容的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04
Android標(biāo)題欄上添加多個(gè)Menu按鈕的實(shí)例
這篇文章主要介紹了Android標(biāo)題欄上添加多個(gè)Menu按鈕的實(shí)例的相關(guān)資料,這里提供簡單實(shí)例說明如何添加多個(gè)menu按鈕的方法,需要的朋友可以參考下2017-09-09
Android 在viewPager中雙指縮放圖片雙擊縮放圖片單指拖拽圖片的實(shí)現(xiàn)思路
本文通過實(shí)例代碼給大家講解了Android 在viewPager中雙指縮放圖片雙擊縮放圖片單指拖拽圖片的實(shí)現(xiàn)思路及解決方案,需要的朋友參考下吧2017-05-05
android IntentService實(shí)現(xiàn)原理及內(nèi)部代碼分享
android IntentService實(shí)現(xiàn)原理及內(nèi)部代碼分享,需要的朋友可以參考一下2013-06-06
Android Studio 創(chuàng)建自定義控件的方法
這篇文章主要介紹了Android Studio 創(chuàng)建自定義控件的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06
Android實(shí)現(xiàn)發(fā)送短信驗(yàn)證碼倒計(jì)時(shí)功能示例
本篇文章主要介紹了Android實(shí)現(xiàn)發(fā)送短信驗(yàn)證碼倒計(jì)時(shí)功能示例,這里整理了詳細(xì)的代碼,有需要的小伙伴可以參考下。2017-03-03
Android 判斷當(dāng)前語言環(huán)境是否是中文環(huán)境
本文主要介紹了Android 判斷當(dāng)前語言環(huán)境是否是中文環(huán)境的方法。具有很好的參考價(jià)值。下面跟著小編一起來看下吧2017-04-04

