Android自定义通知布局Notification,点击Notification导航切换回原Activity

原博: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;

	@Override
	protected 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() + "");
	}
	
	@Override
	protected void onRestart() {
		super.onRestart();
		Log.d(this.getClass().getName(),"onRestart()");
	}
	
	@Override
	protected void onStart() {
		super.onStart();
		Log.d(this.getClass().getName(),"onStart()");
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		Log.d(this.getClass().getName(),"onResume()");
	}

	//Android设备锁屏也将进入onStop()
	@Override
	protected 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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值