消息通知 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

[html] view plaincopy
  1. <activity  
  2.             android:name="zhangphil.pendingintent.MainActivity"  
  3.             android:launchMode="singleTop"  
  4.             android:label="@string/app_name" >  
  5.             <intent-filter>  
  6.                 <action android:name="android.intent.action.MAIN" />  
  7.                 <category android:name="android.intent.category.LAUNCHER" />  
  8.             </intent-filter>  
  9.         </activity>  

0 0
原创粉丝点击