Notification(三)——点Notification后返回当前App所在的Activity

简介: MainActivity如下:package cc.cu;import android.app.Activity;import android.
MainActivity如下:
package cc.cu;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
/**
 * Demo描述:
 * 点击状态栏的通知Notification返回当前App所在的Activity,而不会重新start一个当前Activity.
 * 主要方式是为Intent设置flag
 * intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK| Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
 * 
 * 
 * 1 http://developer.android.com/reference/android/app/PendingIntent.html#FLAG_UPDATE_CURRENT
 * 2 http://blog.csdn.net/vipzjyno1/article/details/25248021
 *   Thank you very much
 *   在资料2中对于Notification作了很全面和详细的介绍.有兴趣的可以看看.
 * 
 * 备注说明:
 * 测试环境Android2.3.6
 *
 */
public class MainActivity extends Activity {
	private Context mContext;
	private Button mButton;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		init();
	}

	private void init() {
		mContext = this;

		mButton = (Button) findViewById(R.id.button);
		mButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {
				Intent intent=new Intent(mContext,AnotherActivity.class);
				startActivity(intent);
			}
		});
	}
}

AnotherActivity如下:
package cc.cu;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
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 AnotherActivity extends Activity {
	private Context mContext;
	private Button mButton;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.other);
		init();
	}

	private void init() {
		mContext = this;

		mButton = (Button) findViewById(R.id.sendNotificationButton);
		mButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {
				sendNotification();
			}
		});
	}

	// 发送通知
	private void sendNotification() {
		Notification notification = new Notification();
		Intent intent = new Intent(mContext, AnotherActivity.class);
		//核心代码
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK| Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
		//如果当前Activity是MainActivity请加上以下两句
		//intent.setAction(Intent.ACTION_MAIN);  
        //intent.addCategory(Intent.CATEGORY_LAUNCHER); 
		PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0,intent, PendingIntent.FLAG_UPDATE_CURRENT);
		notification.icon = R.drawable.ic_launcher;
		notification.defaults = Notification.DEFAULT_SOUND;
		notification.flags |= Notification.FLAG_AUTO_CANCEL;
		notification.tickerText = "第一个通知";
		notification.setLatestEventInfo(mContext, "通知", "来自第按钮触发的通知",pendingIntent);
		NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
		notificationManager.notify(0, notification);
	}

}

main.xml如下:
<RelativeLayout 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"
    >
     <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="100dip"
        android:text="跳转到下一个界面" />
</RelativeLayout>

other.xml如下:
<RelativeLayout 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"
    >
     <Button
        android:id="@+id/sendNotificationButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="100dip"
        android:text="发送通知" />
</RelativeLayout>


相关文章
|
开发工具 Android开发
Appium之获取app的package和activity以及UI界面定位方法
一、获取APP的package(包名)和activity 在使用android自动化测试工具monkeyrunner和appium中启动应用时,需要填写被测程序的包名和启动的Activity,以下有几种查看应用包名package和入口activity名称的方法: 1.
3770 0
|
Android开发
App Inventor 2 Activity启动器技巧:如何查看并启动其他App
Activity启动器(ActivityStarter)如何查看并启动其他App,可以通过解压源码查看,或使用“包名查看器”App查看,不同品牌手机可能略有不同。
812 1
|
Android开发
Android Studio App开发入门之在活动之间传递消息(附源码 超详细必看)(包括显示和隐式Intent,向上一个和下一个Activity发送数据)
Android Studio App开发入门之在活动之间传递消息(附源码 超详细必看)(包括显示和隐式Intent,向上一个和下一个Activity发送数据)
945 0
|
XML 安全 Java
Android Studio App开发入门之活动Activity中为活动补充附加信息讲解及实战(附源码 超详细必看)
Android Studio App开发入门之活动Activity中为活动补充附加信息讲解及实战(附源码 超详细必看)
257 0
|
Android开发
Android Studio APP开发入门之活动Activity中启停活动页面的讲解及实战(附源码,包括Activity的启动结束、生命周期、跳转等)
Android Studio APP开发入门之活动Activity中启停活动页面的讲解及实战(附源码,包括Activity的启动结束、生命周期、跳转等)
493 0
|
Java Android开发
关于android studio开发APP中,给单个Activity设置隐藏上面标题栏的简单方案。
关于android studio开发APP中,给单个Activity设置隐藏上面标题栏的简单方案。
621 0
关于android studio开发APP中,给单个Activity设置隐藏上面标题栏的简单方案。
|
Android开发
Android 音乐APP(六)Activity和Notification通讯
Android 音乐APP(六)Activity和Notification通讯
656 0
Android 音乐APP(六)Activity和Notification通讯
|
监控 算法 Java
【解惑】App处于前台,Activity就不会被回收了?(下)
【解惑】App处于前台,Activity就不会被回收了?
743 0
|
缓存 监控 算法
【解惑】App处于前台,Activity就不会被回收了?(上)
【解惑】App处于前台,Activity就不会被回收了?
632 0
|
Shell Android开发
Appium自动化(5) - 如何获取android app 的Activity 和 Package
Appium自动化(5) - 如何获取android app 的Activity 和 Package
581 0
Appium自动化(5) - 如何获取android app 的Activity 和 Package

热门文章

最新文章