消息通知 Notification 点击切换回原Activity
来源:互联网 发布:工程项目经理收入知乎 编辑:程序博客网 时间:2024/06/07 01:09
一个简单的应用场景:假如用户打开Activity以后,按Home键,此时Activity 进入-> onPause() -> onStop() 不可见。代码在此时机发送一个Notification到通知栏。当用户点击通知栏的Notification后,又重新onRestart() -> onStart() -> onResume() 切换回原Activity。
效果图:
package com.example.notification;import android.app.Activity;import android.app.Notification;import android.app.NotificationManager;import android.app.PendingIntent;import android.content.Intent;import android.os.Bundle;import android.support.v4.app.NotificationCompat;import android.util.Log;import android.view.View;import android.widget.Button;import android.widget.RemoteViews;public class MainActivity extends Activity {private final int NOTIFICATION_ID = 0xa01;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);Log.d(this.getClass().getName(), "onCreate()");setContentView(R.layout.activity_main);Button send = (Button) findViewById(R.id.send);send.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {sendNotification();}});Button clear = (Button) findViewById(R.id.clear);clear.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {clearNotification();}});}private void sendNotification() {NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);mBuilder.setSmallIcon(R.drawable.ic_launcher);// mBuilder.setContentTitle("来自徐华清");// mBuilder.setContentText("烟花易冷,很好听!");Notification notification = mBuilder.build();// 当用户下来通知栏时候看到的就是RemoteViews中自定义的Notification布局RemoteViews rv = new RemoteViews(getPackageName(),R.layout.notification);rv.setImageViewResource(R.id.image, R.drawable.qq);rv.setTextViewText(R.id.title, "清清");rv.setTextViewText(R.id.text, "烟花易冷,很好听!");notification.contentView = rv;// 需要注意的是,作为选项,此处可以设置MainActivity的启动模式为singleTop,避免重复新建onCreate()。Intent intent = new Intent(this, MainActivity.class);// 当用户点击通知栏的Notification时候,切换回MainActivity。PendingIntent pi = PendingIntent.getActivity(this, 0x05, intent,PendingIntent.FLAG_UPDATE_CURRENT);notification.contentIntent = pi;// 缺省设置为当发送通知到通知栏的时候:提示声音+震动notification.defaults = Notification.DEFAULT_SOUND| Notification.DEFAULT_VIBRATE;// 通知的时间notification.when = System.currentTimeMillis();// 发送到手机的通知栏notificationManager.notify(NOTIFICATION_ID, notification);}private void clearNotification() {NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);notificationManager.cancel(NOTIFICATION_ID);}}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:orientation="vertical" tools:context="com.example.notification.MainActivity" > <Button android:id="@+id/send" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="发送通知" /> <Button android:id="@+id/clear" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="清除通知" /> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
<?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>
在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>
0 0
- 消息通知 Notification 点击切换回原Activity
- Android自定义通知布局Notification,点击Notification导航切换回原Activity
- Android自定义通知布局Notification,点击Notification导航切换回原Activity
- android 自定义通知布局Notification,点击Notification导航切换回原Activity
- Android自定义通知布局Notification,点击Notification导航切换回原Activity
- 点击notification返回原Activity而不是创建新Activity
- android点击系统通知Notification,进入指定的activity
- Android -- Notification,如何向通知栏发送通知,点击通知开启相应的Activity
- Activity之Notification(通知)
- anroid Notification消息通知
- Android 消息通知-Notification
- Android 消息通知-Notification
- Notification实现消息通知
- js消息通知Notification
- Android 中的消息通知Notification
- 消息栏通知(Notification)介绍
- 消息栏通知(Notification)介绍
- Android的消息通知--Notification
- iOS6、7各自的导航栏、状态栏
- qt+vs第二篇(designer的使用)
- javaweb学习总结(三十九)——数据库连接池
- Zookeeper配置文件(全)
- Docker 监控实战
- 消息通知 Notification 点击切换回原Activity
- 显示器的分辨率被锁定的可选解决方案
- Android持久化保存cookie
- Objective-C RunTime机制(1)
- 五个免费UML建模工具推荐
- 一、Camera显示之app实现简单camera
- 黑马程序员——JAVA基础——网络编程
- Windows和Linux下取得系统时间(毫秒级)
- tomcat缓存问题