效果圖:html
學習:android
1.靜態註冊實現開機啓動app
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> <receiver android:name="com.example.alarmmanagerdemo.AlarmReceiver" > <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> </intent-filter> </receiver>
2.通知的建立ide
step1:須要一個NotificationManager來對通知進行管理學習
NotificationManager manager = (NotificationManager)context.getSystemService(android.content.Context.NOTIFICATION_SERVICE); ui
step2:建立一個Notification對象,這個對象用於存儲通知所需的各類信息。spa
NotificationCompat.Builder builder = new NotificationCompat.Builder(context); builder.setContentTitle("Mission Of Everyday").setContentText("Remeber Vocabulary, Exercise, Speaking English").setSmallIcon(R.drawable.sun).setDefaults(Notification.DEFAULT_ALL).setAutoCancel(true).setSubText(" For Dream!");
step3:調用NotificationManager的notify()讓通知顯示出來。.net
manager.notify(1, builder.build());
源碼:code
import android.app.Notification; import android.app.NotificationManager; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.support.v4.app.NotificationCompat; import android.widget.Toast; public class AlarmReceiver extends BroadcastReceiver { private NotificationManager manager; @Override public void onReceive(Context context, Intent intent) { //Toast.makeText(context, "闂歸搩鍝嶄簡, 鍙互鍋氱偣浜嬫儏浜唦~", Toast.LENGTH_LONG).show(); Toast.makeText(context, "Boot Complete", Toast.LENGTH_LONG).show(); manager = (NotificationManager)context.getSystemService(android.content.Context.NOTIFICATION_SERVICE); NotificationCompat.Builder builder = new NotificationCompat.Builder(context); builder.setContentTitle("Mission Of Everyday").setContentText("Remeber Vocabulary, Exercise, Speaking English").setSmallIcon(R.drawable.sun).setDefaults(Notification.DEFAULT_ALL).setAutoCancel(true).setSubText(" For Dream!"); manager.notify(1, builder.build()); } }
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.alarmmanagerdemo" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="18" /> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> <application android:allowBackup="true" android:icon="@drawable/icon" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.alarmmanagerdemo.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <receiver android:name="com.example.alarmmanagerdemo.AlarmReceiver" > <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> </intent-filter> </receiver> </application> </manifest>
Demo下載地址:http://pan.baidu.com/s/1hrWY5kK
xml
參考:1.http://www.runoob.com/w3cnote/android-tutorial-notification.html
2.《第一行代碼》