点击通知启动App
来源:互联网 发布:2013cctv网络春晚韩庚 编辑:程序博客网 时间:2024/05/18 02:06
我们在使用别的软件的时候,经常会看到在手机的最头部有一个小图标,表示程序依然在启动着。比如一些音乐播放软件天天动听,还有服务程序正点工具箱,还有腾讯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>
- 点击通知启动App
- 点击通知栏里的通知启动App
- 检测是否通过点击通知来启动App
- 点击通知栏启动程序
- 安卓开发,点击通知栏进入指定页面及判断App是否启动
- android 点击通知栏消息打开activity,如果app未运行先启动app或打开activity返回后再启动app
- Android 点击链接启动APP
- app从点击到启动
- 【Android】信鸽推送通知栏点击之重复打开APP&重复从Laucher主入口启动问题
- Android缩小到通知栏,再点击通知栏启动
- Android 点击通知栏中的通知启动应用
- 通知----点击通知启动activity时保留导航
- 本地通知启动 app 的状态
- React native添加iOS远程通知点击通知打开APP在APP杀死状态下闪退解决办法
- Android推送遇到的问题--app关闭后,点击通知后点击通知后app未初始化的问题
- 浏览器点击链接启动安卓APP
- Android中点击通知栏图标,启动一个应用
- Android中点击通知栏图标,启动一个应用
- 第八周项目5 计数的模式匹配
- iphone 与 iPad在开发中的区别以及iPad中的UIPopoverController的使用
- Spark求数据集中同一主键记录中的最新数据
- 结构化图形绘制利器Graphviz
- 遗传算法--用python进化出chrome浏览器图标
- 点击通知启动App
- 二十年未见之数据造假现象,出路何在?
- Elasticsearch基础查询
- C#实现DDE的简便方法
- jsp小结12 - 9个内置对象05 page
- 利用python对服务器做巡检工作
- Socket 编程(一)
- Rational Rose简明实用教程
- 开始学习Python for Data Analysis