消息通知 Notification 点击切换回原Activity

本文介绍了当用户从Activity按Home键后,通过发送Notification,点击通知如何使应用回到原来的Activity。关键在于设置Activity的启动模式为singleTop,确保点击Notification时不会创建新的实例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


一个简单的应用场景:假如用户打开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;
	

	@Override
	protected 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() {

			@Override
			public void onClick(View v) {
				sendNotification();

			}
		});

		Button clear = (Button) findViewById(R.id.clear);
		clear.setOnClickListener(new View.OnClickListener() {

			@Override
			public 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 plain copy
  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>  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值