标签:
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;
publicclassMyNotificationActivityextendsActivity{
privateButton btn_notify1;
p
rivateNotificationManager nManager;privateNotification notification ;
@Override
protectedvoid onCreate(Bundle savedInstanceState){
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_notification);
//得到notification管理器
nManager =(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
btn_notify1 =(Button)findViewById(R.id.btn_notify1);
btn_notify1.setOnClickListener(newOnClickListener(){
@Override
publicvoid onClick(View v){
// TODO Auto-generated method stub
PendingIntent piIntent =PendingIntent.getActivity(MyNotificationActivity.this,1,newIntent(MyNotificationActivity.this,FormActivity.class),1);
/*Notification notification = new Notification(R.drawable.p2409, "You have a message", System.currentTimeMillis());
notification.setLatestEventInfo(MyNotificationActivity.this, "Racoon", "Love U", piIntent);
*/
//创建notification实例
notification =newNotification.Builder(MyNotificationActivity.this)
.setContentText("Love U")
.setContentTitle("little Racoon")
.setTicker("You have a new message")
.setSmallIcon(R.drawable.peasy)//状态栏的图标
.setContentIntent(piIntent)
.getNotification();
notification.contentView =newRemoteViews(getPackageName(), R.layout.layout_customnotification);
//把notification发布到状态栏
nManager.notify(1, notification);
}
});
}
@Override
protectedvoid onStop(){
// TODO Auto-generated method stub
nManager.cancelAll();
super.onStop();
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/btn_notify1"
style="?android:attr/buttonStyleSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Common notify"/>
标签:
原文地址:http://www.cnblogs.com/izhanjun/p/4225153.html