android 开机时在导航栏提示信息

7,269 views

实现开机时在导航栏提示信息的步骤:

(1.)开机启动一个广播通知事件对象,可以不用启动一个后台线程服务;

(2.)在事件通知对象中实现通知的功能,开机时在导航栏启动一个提醒信息;

(3.)点击该事件提醒后进入相应的界面,并取消通知(使导航栏提示信息清空)。

1.开机启动一个服务需要实现一个BroadcastReceiver广播事件,重写onReceive()方法,并在AndroidManifest.xml文件中注册相应的事件。

注册NotificationReceiver:

<!-- 开机启动的广播 -->
<receiver android:name=".NotificationReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<category android:name="android.intent.category.HOME" />
</intent-filter>
</receiver>

2.重写BroadcastReceiver onReceive()方法,主要实现android 导航栏通知事件的功能,关键代码:

//这是通知的ID,自定义,在取消该通知时,需要该ID
final int NOTICE_ID=10000;
//获得通知管理器
final NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
//初始化通知对象 第一个参数:通知的图标 第一个参数:通知的状态栏显示的提示 第一个参数:通知显示的时间
Notification notification = new Notification(R.drawable.icon, "开机提醒", System.currentTimeMillis());
//点击通知后的Intent
PendingIntent intent2 = PendingIntent.getActivity(context, 0, new Intent(context, NotificationUIAcitvity.class), 0);
//设置通知信息
notification.setLatestEventInfo(context, "开机提醒通知", "您有更新的通知,请点击查看", intent2);
//通知
manager.notify(NOTICE_ID, notification);

3.点击导航栏通知消息后进入指定的界面并清除导航栏的信息(取消通知):

final int NOTICE_ID=10000;
//点击后取消通知
final NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
//取消通知
manager.cancel(NOTICE_ID);

完整代码:

1.NotificationReceiver.java

package com.notification.main;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class NotificationReceiver extends BroadcastReceiver{
//这是通知的ID,自定义,在取消该通知时,需要该ID
private final int NOTICE_ID=10000;
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
//获得通知管理器
final NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
//初始化通知对象 第一个参数:通知的图标 第一个参数:通知的状态栏显示的提示 第一个参数:通知显示的时间
Notification notification = new Notification(R.drawable.icon, "开机提醒", System.currentTimeMillis());
//点击通知后的Intent
PendingIntent intent2 = PendingIntent.getActivity(context, 0, new Intent(context, NotificationUIAcitvity.class), 0);
//设置通知信息
notification.setLatestEventInfo(context, "开机提醒通知", "您有更新的通知,请点击查看", intent2);
//通知
manager.notify(NOTICE_ID, notification);
}
}

跳转的界面

2.NotificationUIAcitvity.java

package com.notification.main;

import android.app.Activity;
import android.app.NotificationManager;
import android.os.Bundle;

public class NotificationUIAcitvity extends Activity{
private final int NOTICE_ID=10000;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
//加载通知界面
setContentView(R.layout.notice);
//点击后取消通知
final NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
//取消通知
manager.cancel(NOTICE_ID);
}
}

应用程序主界面

3.BootNotificationActivity

package com.notification.main;

import android.app.Activity;
import android.os.Bundle;

public class BootNotificationActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//这是程序主界面
setContentView(R.layout.main);
}
}

4.AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.notification.main"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="7" />

<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name="BootNotificationActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- 点击导航栏通知后跳转的界面 -->
<activity android:name="NotificationUIAcitvity"/>
<!-- 开机启动的广播 -->
<receiver android:name=".NotificationReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<category android:name="android.intent.category.HOME" />
</intent-filter>
</receiver>
</application>
</manifest>

如图-1

图-1 开机启动提醒功能

源码下载:BootNotification.zip

This entry was posted in Uncategorized by Charlie. Bookmark the permalink.

One thought on “android 开机时在导航栏提示信息

  1. Pingback: android开机时在导航栏提示信息 - 移动端开发 - 开发者

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>