效果图:
学习:
1.静态注册实现开机启动
<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.通知的创建
step1:需要一个NotificationManager来对通知进行管理
NotificationManager manager = (NotificationManager)context.getSystemService(android.content.Context.NOTIFICATION_SERVICE);
step2:创建一个Notification对象,这个对象用于存储通知所需的各种信息。
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()让通知显示出来。
manager.notify(1, builder.build());
源码:
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
参考:1.http://www.runoob.com/w3cnote/android-tutorial-notification.html
2.《第一行代码》