使用Notification中的contentView可以自定义通知视图。点击通知栏的按钮可以启动到另一个Activity界面。
下面是程序代码:
MainActivity.java
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.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RemoteViews;
public class MainActivity extends Activity {
NotificationManager nm;
Notification notification;
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button bt1 = (Button)findViewById(R.id.button1);
bt1.setOnClickListener(bt1lis);
Button bt2 = (Button)findViewById(R.id.button2);
bt2.setOnClickListener(bt2lis);
nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
notification = new Notification(R.drawable.ic_launcher, "图标边的文字", System.currentTimeMillis());
notification.contentView = new RemoteViews(getPackageName(), R.layout.view); //自定义视图
PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), //启动aboutActivity
0,
new Intent(getApplicationContext(), aboutActivity.class),
0);
notification.contentView.setOnClickPendingIntent(R.id.button3, contentIntent); //绑定点击自定义视图中按钮的动作
}
OnClickListener bt1lis = new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
showNotification();
}
};
OnClickListener bt2lis = new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
nm.cancel(R.id.textView1);
}
};
public void showNotification() {
nm.notify(R.id.textView1, notification);
}
}
activity_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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="28dp"
android:text="Notification测试" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_below="@+id/button1"
android:layout_marginTop="27dp"
android:text="清除Notification" />
</RelativeLayout>
view.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:padding="10dp" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/talkback" />
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Talkback快速启动 开启" />
</LinearLayout>
ActivityManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.notification"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.notification.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.notification.aboutActivity">
</activity>
</application>
</manifest>