MainActivity.java
package com.android.notification.activity;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
private Button sendButton;
private Button openAlertButton;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
sendButton=(Button) this.findViewById(R.id.sendButton);
openAlertButton= (Button) this.findViewById(R.id.openAlertButton);
sendButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// 如何发送通知?
//1. 获取通知管理器
NotificationManager notificationManager = (NotificationManager) MainActivity.this.getSystemService(Context.NOTIFICATION_SERVICE);
//2.创建一个通知
//int icon=android.R.drawable.ic_menu_call;
int icon=R.drawable.yyc;
long when=System.currentTimeMillis();//android 操作系统时间
Notification notification=new Notification(icon, null, when);
// 3.发送通知
String contentTitle="开会通知";
String contentText="下午14点开会";
Intent intent=new Intent(MainActivity.this, OtherActivity.class);
// intent携带参数
intent.putExtra("contentTitle", contentTitle);
intent.putExtra("contentText", contentText);
PendingIntentcontentIntent = PendingIntent.getActivity(MainActivity.this, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT) ;
notification.setLatestEventInfo(MainActivity.this, contentTitle, contentText, contentIntent);
notificationManager.notify(0, notification);//向操作系统发送此通知
}
});
openAlertButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
new AlertDialog.Builder(MainActivity.this).setTitle("信息提示").setMessage("确定删除吗?")
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "确定", Toast.LENGTH_SHORT).show();
}
}).setNegativeButton("取消", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "取消", Toast.LENGTH_SHORT).show();
}
})
.setCancelable(false)
.show();
}
});
}
}
OtherActivity.java
package com.android.notification.activity;
import android.app.Activity;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class OtherActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
// 加载 主窗口
super.onCreate(savedInstanceState);
this.setContentView(R.layout.other);
Intent intent = this.getIntent();
//获取intent参数内容
String contentTitle=intent.getStringExtra("contentTitle");
String contentText=intent.getStringExtra("contentText");
TextView resultTextView = (TextView) this.findViewById(R.id.resultTextView);
resultTextView.setText("标题:"+contentTitle+"\n"+"内容:"+contentText);
//NotificationManager notificationManager=(NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
//notificationManager.cancel(0);
}
}
AndroidManifest.xml需要配置一句
<activity
android:name="OtherActivity" android:label="other界面"
/>
string.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, MainActivity!</string>
<string name="app_name">通知</string>
<string name="sendNotification">发送通知</string>
<string name="openAlert">打开对话框</string>
</resources>
other.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="test"
android:id="@+id/resultTextView"
/>
</LinearLayout>
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/sendNotification"
android:id="@+id/sendButton"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/openAlert"
android:id="@+id/openAlertButton"
/>
</LinearLayout>