Android中使用Notification并通过点击Notification重新启动Activity

我们在使用别的软件的时候,经常会看到在手机的最头部有一个小图标,表示程序依然在启动着。比如一些音乐播放软件天天动听,还有服务程序正点工具箱,还有腾讯QQ等。本文就带您来了解一些如何做到这些功能。

相关知识点为:
NotificationManager,getSystemService,NOTIFICATION_SERVICE,getBaseContext,Notification,

Class.forName,setFlags,FLAG_ACTIVITY_SINGLE_TOP,PendingIntent,setLatestEventInfo,FLAG_ONGOING_EVENT,notify,cancel.
效果图:

 

我们先上代码:

//两个参数,第一个为一个图标的ID,第二个为一个字符串对应的ID
public void StartNM(int icoID,int strID)
{
//获取到系统服务中的通知服务NOTIFICATION_SERVICE
nManager = (NotificationManager)getSystemService(getBaseContext().NOTIFICATION_SERVICE);
//将字符串传化为Notification所需用的格式CharSequence
CharSequence str = getBaseContext().getText(strID);
//新建一个通知对象
Notification notification = new Notification(icoID,str,System.currentTimeMillis());

Intent intent = null;
try {
intent = new Intent(this,Class.forName("com.jouhu.gpsservice.GPSServiceActivity"));
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
//PendingIntent为一个特殊的Intent,通过getBroadcast或者getActivity或者getService得到.
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
notification.setLatestEventInfo(this, str, str, pendingIntent);
notification.flags |= Notification.FLAG_ONGOING_EVENT;
//启动通知事件
nManager.notify(strID, notification);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
//
public void StopNM(int strID)
{
//获取到系统服务中的通知服务NOTIFICATION_SERVICE
nManager = (NotificationManager)getSystemService(getBaseContext().NOTIFICATION_SERVICE);
nManager.cancel(strID);
}

在我们的按钮事件里面通过以下方法就可以启动和关闭了。

StartNM(R.drawable.logo,R.string.app_name);
//以及
StopNM(R.string.app_name);

里面还涉及到一些步骤。
1 制作一个16 * 16 的logo.PNG文件放到res/drawable-hdpi,res/drawable-ldpi,res/drawable-mdpi文件夹中,刷新本项目,在gen/com.jouhu/gpsservice中就会生成一个logo的id了,程序中就可以使用R.drawable.logo调用了
2 注意,我们需要使用类的全名来创建这个Intent,否则会出现找不到的情况。

new Intent(this,Class.forName("com.jouhu.gpsservice.GPSServiceActivity"));

3 注意要加这个代码

intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

the activity will not be launched if it is already running at the top of the history stack.
解释为Activity不会被多次执行。
4 在开发音乐类程序的时候,我们需要考虑当Activity关闭的时候,下次启动音乐程序,需要继续当前的音乐,所有需要将当前的信息传递到重新启动的Activity中。
How to send parameters from a notification-click to an activity?文章提到了解决这个问题的思路。

For managing if the activity is already running you have two ways:
>>如果Activity已经运行有两种方法

Add FLAG_ACTIVITY_SINGLE_TOP flag to the Intent when launching the activity, and then in the activity class implement onNewIntent(Intent intent) event handler, that way you can access the new intent that was called for the activity (which is not the same as just calling getIntent(), this will always return the first Intent that launched your activity.
>>当启动Activity的时候添加FLAG_ACTIVITY_SINGLE_TOP到Intent(本代码已经包含),在Activity类中实现onNewIntent的事件处理。
Same as number one, but instead of adding a flag to the Intent you must add “singleTop” in your activity AndroidManifest.xml.

<activity
android:name=".ArtistActivity"
android:label
="Artist"
android:launchMode
="singleTop">
</activity>

项目的图效果如下:

参考文章:
1 http://www.cnblogs.com/xianzuoqiaoqi/archive/2011/09/16/2178886.html
2 http://blog.csdn.net/vincent_czz/article/details/6058385
3 http://stackoverflow.com/questions/1711785/android-single-top-launch-mode-and-onnewintent-method
4 http://developer.android.com/guide/topics/ui/notifiers/notifications.html

 

本文同发地址:http://doandroid.info/?p=1377



posted on 2011-11-14 21:59  移动开发团队  阅读(6395)  评论(0编辑  收藏  举报

导航