1.创建一个activity用于传入notification的内容
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/shorttitle"/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/title"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/title"/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/shorttitle"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/content"/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/content"/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="发送"
android:onClick="send"/>
</LinearLayout>
2.代码编写
package com.example.notifitiondemo;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends Activity {
private EditText shorttitle;
private EditText title;
private EditText content;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
shorttitle = (EditText)findViewById(R.id.shorttitle);
title = (EditText)findViewById(R.id.title);
content = (EditText)findViewById(R.id.content);
}
public void send(View v){
String tickerText = shorttitle.getText().toString();
String titleText = title.getText().toString();
String contentText = content.getText().toString();
System.out.println(tickerText);
System.out.println(titleText);
System.out.println(contentText);
//获取图片ID
int icon = android.R.drawable.stat_notify_call_mute;
Notification notification = new Notification(
icon,//图标
tickerText, //摘要
System.currentTimeMillis());//时间
Intent intent = new Intent(Intent.ACTION_CALL,Uri.parse("tel:5556"));
//用来激活activity对象
//步骤2
PendingIntent pendingIntent = PendingIntent.getActivity(this,
10,//请求码
intent, 0);
//步骤1
notification.setLatestEventInfo(this, //上下文对象
titleText,//标题
contentText,//内容
pendingIntent);//点击通知后要激活的组件
//设置声音
notification.defaults = Notification.DEFAULT_SOUND;
//设置 如果通知被点击 则通知自动取消
notification.flags = Notification.FLAG_AUTO_CANCEL;
//通知管理器
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
//发送通知
manager.notify(100, notification);
}
}
3.效果图