Android自定义通知布局Notification,点击Notification导航切换回原Activity
来源:互联网 发布:淘宝特种经营报名入口 编辑:程序博客网 时间:2024/06/02 00:06
原博:http://blog.csdn.net/zhangphil
一个简单的应用场景:假如用户打开Activity以后,按Home键,此时Activity 进入-> onPause() -> onStop() 不可见。代码在此时机发送一个Notification到通知栏。当用户点击通知栏的Notification后,又重新onRestart() -> onStart() -> onResume() 切换回原Activity。
package zhangphil.pendingintent;import android.os.Bundle;import android.support.v4.app.NotificationCompat;import android.util.Log;import android.widget.RemoteViews;import android.widget.TextView;import android.app.Activity;import android.app.Notification;import android.app.NotificationManager;import android.app.PendingIntent;import android.content.Context;import android.content.Intent;public class MainActivity extends Activity {private final int NOTIFICATION_ID = 0xa01;private final int REQUEST_CODE = 0xb01;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);Log.d(this.getClass().getName(),"onCreate()");setContentView(R.layout.activity_main);TextView tv = (TextView) findViewById(R.id.textView);tv.setText(System.currentTimeMillis() + "");}@Overrideprotected void onRestart() {super.onRestart();Log.d(this.getClass().getName(),"onRestart()");}@Overrideprotected void onStart() {super.onStart();Log.d(this.getClass().getName(),"onStart()");}@Overrideprotected void onResume() {super.onResume();Log.d(this.getClass().getName(),"onResume()");}//Android设备锁屏也将进入onStop()@Overrideprotected void onStop() {super.onStop();Log.d(this.getClass().getName(),"onStop()");sendNotification(this);}private void sendNotification(Context context) {NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context);// 此处设置的图标仅用于显示新提醒时候出现在设备的通知栏mBuilder.setSmallIcon(R.drawable.ic_launcher);// mBuilder.setContentTitle("通知的标题");// mBuilder.setContentText("通知的内容");Notification notification = mBuilder.build();// 当用户下来通知栏时候看到的就是RemoteViews中自定义的Notification布局RemoteViews contentView = new RemoteViews(context.getPackageName(),R.layout.notification);contentView.setImageViewResource(R.id.image, R.drawable.ic_launcher);contentView.setTextViewText(R.id.title, "标题要长......");contentView.setTextViewText(R.id.text, "内容要短......");notification.contentView = contentView;// 发送通知到通知栏时:提示声音 + 手机震动 + 点亮Android手机呼吸灯。// 注意!!(提示声音 + 手机震动)这两项基本上Android手机均支持。// 但Android呼吸灯能否点亮则取决于各个手机硬件制造商自家的设置。notification.defaults = Notification.DEFAULT_SOUND| Notification.DEFAULT_VIBRATE | Notification.DEFAULT_LIGHTS;// 点击notification自动消失notification.flags = Notification.FLAG_AUTO_CANCEL;// 通知的时间notification.when = System.currentTimeMillis();// 需要注意的是,作为选项,此处可以设置MainActivity的启动模式为singleTop,避免重复新建onCreate()。Intent intent = new Intent(context, MainActivity.class);// 当用户点击通知栏的Notification时候,切换回MainActivity。PendingIntent pi = PendingIntent.getActivity(context, REQUEST_CODE,intent, PendingIntent.FLAG_UPDATE_CURRENT);notification.contentIntent = pi;// 发送到手机的通知栏notificationManager.notify(NOTIFICATION_ID, notification);}}
需要注意的是,默认Android的Activity为标准模式,即每次都new一个新的Activity出来,不是原先的Activity,在本例中,可以观察到MainActivity中的onCreate()如果不修改启动模式,则每次本调用每次TextView显示的时间不同(递增),所有为了使用原来的Activity、避免重复new一个新的出来,需要:
在AndroidManifest.xml中修改MainActivity启动模式为:singleTop
<activity android:name="zhangphil.pendingintent.MainActivity" android:launchMode="singleTop" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>
新建notification.xml文件源代码:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <ImageView android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_marginRight="10dp" /> <TextView android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@id/image" /> <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/title" android:layout_toRightOf="@id/image" /></RelativeLayout>
activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" /></RelativeLayout>
0 0
- Android自定义通知布局Notification,点击Notification导航切换回原Activity
- Android自定义通知布局Notification,点击Notification导航切换回原Activity
- android 自定义通知布局Notification,点击Notification导航切换回原Activity
- Android自定义通知布局Notification,点击Notification导航切换回原Activity
- 消息通知 Notification 点击切换回原Activity
- Android自定义Notification通知
- Android的Notification的自定义布局按钮点击+点击按钮收回系统通知栏
- Android自定义Notification布局
- android点击系统通知Notification,进入指定的activity
- 状态栏Notification | 点击通知后跳转(未写) | 自定义Notification布局(未写) | 封装
- 59 Android Notification 自定义通知
- android-Notification点击跳转Activity
- Android -- Notification,如何向通知栏发送通知,点击通知开启相应的Activity
- Android Notification自定义布局实现
- Android-paddingIntent.Notification.自定义布局
- android自定义通知,android自定义Notification
- 点击notification返回原Activity而不是创建新Activity
- Android 通知栏Notification点击跳转无效
- Git命令2
- Introduction to AT commands and its uses
- Syntax error, parameterized types are only available if source level is 1.5
- 应用程序无法启动,因为应用程序的并行配置不正确。有关详细信息,请参阅应用程序事件日志,或使用命令行sxstrace.exe工具。解决方法
- Zookeeper集群安装及命令
- Android自定义通知布局Notification,点击Notification导航切换回原Activity
- android插件话和hotfix
- 计算机视觉著名数据集CV Datasets
- 使用视图控制器容器(翻译自苹果官方文档 Implementing a Container View Controller)
- 设计模式之责任链模式(Chain of Responsibity)
- 《剑指Offer》读书笔记03:从尾到头打印链表
- AMD与CDM RequireJS与SeaJS CommonJS 的认识
- linux经验之用scp远程传输文件/文件夹
- 深度与广度 总结