MainActivity.java:
package com.example.notificationdemo; import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.app.Notification;
import android.app.Notification.Builder;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
import android.os.Build; public class MainActivity extends Activity {
Button btn_cancel;
Button btn_generate;
Notification notification;
PendingIntent pintent;
Intent intent;
NotificationManager manager;// 后面要用 @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_cancel = (Button) findViewById(R.id.btn_cancel);
btn_generate = (Button) findViewById(R.id.btn_generate);
btn_generate.setOnClickListener(new MyListener());
btn_cancel.setOnClickListener(new MyListener()); } public class MyListener implements OnClickListener { @Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_generate:
System.out.println("=========btn_generate=====");
GenerateNotification();
break;
case R.id.btn_cancel:
System.out.println("=========btn_cancel=====");
CancelNotification();
break; }
} } private void GenerateNotification() { intent = new Intent(this, SecondActivity.class);
pintent = PendingIntent.getActivity(this, 0, intent, 0);
Builder builder = new Builder(this);
builder.setSmallIcon(R.drawable.ic_launcher);
builder.setTicker("这是手记状态栏提示");
builder.setWhen(System.currentTimeMillis());
builder.setContentTitle("woshi biaoti");
builder.setContentText("标题内容我是");
builder.setContentIntent(pintent);
builder.setDefaults(Notification.DEFAULT_SOUND);
builder.setDefaults(Notification.DEFAULT_LIGHTS);
// builder.getNotification();//4.0以及以下版本用这个获取notification
Notification notification = builder.build();// 4.1以及以上版本用这个
Toast.makeText(this, "生成通知", 50).show();
manager = (NotificationManager) this
.getSystemService(NOTIFICATION_SERVICE);
manager.notify(23, notification);// 发出通知
} private void CancelNotification() {
// if(manager.equals("")||manager==null){//不能这么判断,因为加入manager为空,那么null.equals("")肯定就会空指针异常
if (manager == null) {
Toast.makeText(this, "亲,暂时没有消息,所以无法取消", 50).show();
return;
} else {
Toast.makeText(this, "取消", 50).show();
manager.cancel(23);
} }
}
SecondActivity.java
package com.example.notificationdemo; import android.app.Activity;
import android.os.Bundle; public class SecondActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_act);
} }
activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.notificationdemo.MainActivity"
tools:ignore="MergeRootFrame" > <Button
android:id="@+id/btn_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:text="取消通知" /> <Button
android:id="@+id/btn_generate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:text="生成通知"
android:layout_below="@+id/btn_cancel" /> </RelativeLayout>
second_act.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.notificationdemo.MainActivity"
tools:ignore="MergeRootFrame" > <TextView
android:id="@+id/tv01"
android:text="我是第二页"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</RelativeLayout>
清單文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.notificationdemo"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.FLASHLIGHT"/>
<uses-permission android:name="android.permission.VIBRATE"/> <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.notificationdemo.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.notificationdemo.SecondActivity"
android:label="@string/app_name" >
</activity>
</application> </manifest>
效果圖:
遇到一个奇怪的事情:
如下:
package com.example.notificationdemo; import android.app.Activity;
import android.app.Notification;
import android.app.Notification.Builder;
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.Toast; public class SecondActivity extends Activity implements OnClickListener {
Button btn;
PendingIntent pintent;
Intent intent;
NotificationManager manager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_act);
System.out.println("second:" + Thread.currentThread().getName());
Toast.makeText(this, "second:" + Thread.currentThread().getName(), 21)
.show();
btn = (Button) findViewById(R.id.sec_generate);
Toast.makeText(this, "this is the second,begin to go back", 12).show();
btn.setOnClickListener(this);
System.out.println("第二页的,btn is onclicked");
} @Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.sec_generate:
intent = new Intent(this,MainActivity.class);
pintent = PendingIntent.getActivity(this, 0, intent, 0);//跳回第一页用
Builder builder = new Builder(this);
builder.setContentText("京东啊啊啊");
builder.setContentTitle("GO BACK");
builder.setSmallIcon(R.drawable.ic_launcher);//不设置SmallICon就没法跳转
// Notification notification = builder.build();
builder.setTicker("我是ticker");
builder.setContentIntent(pintent);
builder.setDefaults(Notification.DEFAULT_SOUND);
builder.setDefaults(Notification.DEFAULT_LIGHTS);
Notification notification = builder.build();//一定要放在setContentIntent()后
manager = (NotificationManager) this.getSystemService(NOTIFICATION_SERVICE);
Toast.makeText(this, "back to the mainactivity", 12).show();
manager.notify(1, notification);
System.out.println("=============se===========");
break;
}
} }
其中的builder.setSmallIcon(R.drawable.ic_launcher);如果注释掉,那么就不会生成相应的通知消息。暂时没找到原因,不知道为什么。