import android.app.Notification;
import android.app.Notification.Builder;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
public class HelloBroadcastReceiver extends BroadcastReceiver{
private NotificationManager notificationManager = null;
private Context context;
int count = 0; //有时service会发出多条通知,我们用一个计数器来模拟不同的通知
private static final int NOTIFY_ME_ID=1337; //通知管理器通过通知ID来标识不同的通知,创建和删除通知是需要提供通知号。
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
System.out.println("0");
this.context=context;
showNotification(intent);
}
private void showNotification(Intent intent) {
count ++;
Resources res =context.getResources();
notificationManager=
(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent pendingIntent=PendingIntent.getActivity(context, 0, intent, 0);
Notification notification = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setTicker("showNormal").setContentInfo("contentInfo")
.setContentTitle("ContentTitle").setContentText("ContentText")
.setNumber(count)
.setContentIntent(pendingIntent)
.setAutoCancel(true).setDefaults(Notification.DEFAULT_ALL)
.build();
notificationManager.notify(NOTIFY_ME_ID, notification);
}
public void notifyClear( ){
notificationManager.cancel( NOTIFY_ME_ID );
}
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
package com.example.fdvasdasfaf;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class CdlRegisterActivity extends Activity {
private Context mContext;
private Button btnSendBroadcast;
private TextView etBroadcastContent;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.register_page);
mContext=this;
btnSendBroadcast= (Button)findViewById(R.id.btn_sendbroadcast);
btnSendBroadcast.setOnClickListener(new SendBroadcastClickListener() );
etBroadcastContent=(TextView) findViewById(R.id.et_broadcastContent);
}
@Override
protected void onSaveInstanceState( Bundle outState){
super.onSaveInstanceState(outState);
}
private class SendBroadcastClickListener implements OnClickListener{
@Override
public void onClick(View arg0) {
String content=etBroadcastContent.getText().toString().trim();
if(content.length()<1){
Toast.makeText(mContext, etBroadcastContent.getHint(), 1).show();
return ;
}
// TODO Auto-generated method stub
Intent intent=new Intent();
intent.setAction("android.intent.action.HelloBroadcastTest");
intent.putExtra("content",content);
sendBroadcast(intent);
}
}
}
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
<receiver android:name=".HelloBroadcastReceiver">
<intent-filter >
<action android:name="android.intent.action.HelloBroadcastTest" />
</intent-filter>
</receiver>