通知的创建 Notification
管理通知NotificationManager
NotificationManager notifyM = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE) ;注意事项: 添加两个权限
震动权限<uses-permission android:name="android.permission.VIBRATE"/>
闪光灯权限<uses-permission android:name="android.permission.FLASHLIGHT"/>
这段代码中包含上一篇文章中介绍的Service的使用
MainActivity代码
public class MainActivity extends Activity implements OnClickListener {
private EditText et ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et = (EditText) findViewById(R.id.main_et) ;
}
private Intent serviceIntent ;
@Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.main_btn_start :
if(null == serviceIntent) {
serviceIntent = new Intent(this , DemoService.class) ;
}
startService(serviceIntent) ;
et.append("启动Service" + Math.random() + "\n") ;
break;
case R.id.main_btn_stop :
if(null != serviceIntent) {
stopService(serviceIntent) ;
}
et.append("停止Service" + Math.random() + "\n") ;
break;
case R.id.main_btn_down1 :
if(null == serviceIntent) {
serviceIntent = new Intent(this , DemoService.class) ;
}
serviceIntent.putExtra("url", "urlA") ;
startService(serviceIntent) ;
break ;
case R.id.main_btn_down2 :
if(null == serviceIntent) {
serviceIntent = new Intent(this , DemoService.class) ;
}
serviceIntent.putExtra("url", "urlB") ;
startService(serviceIntent) ;
break;
}
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
// if(null != serviceIntent) {
// stopService(serviceIntent) ;
// }
}
}
DomeService代码:
public class DemoService extends Service {
private String url = null ;
private int flag = 1 ;
// Service 被初始启动时调用该方法
@Override
public void onCreate() {
Log.v("Service", "onCreate") ;
}
// Service 每次被start时会调用该方法
// 如果通过startService()方式启动,在onStartCommand中描述Service要执行的任务
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
flag++ ;
Log.v("Service", "onStartCommand") ;
// 接收url
url = intent.getStringExtra("url") ;
if(null == url) {
return super.onStartCommand(intent, flags, startId);
}
// 执行下载
down(url , flag) ;
return super.onStartCommand(intent, flags, startId);
}
/**
* 模拟下载
*/
private void down(final String url , final int flag) {
// 下载,自己开启线程
new Thread(){
public void run() {
try {
Thread.sleep(5 * 1000) ;
} catch (InterruptedException e) {
e.printStackTrace();
}
Log.v("Service" , url) ;
// 发出通知
notify(url , flag) ;
// stopSelf();
}
/**
* 下载结束,发出<strong>通知 Notification</strong>
* @param url
*/
private void notify(String url , int flag) {
// Notification notify = new Notification(icon, tickerText, when);
Notification notify = new Notification() ;
// 状态栏通知图标和文字
notify.icon = R.drawable.ic_launcher ;
notify.tickerText = "下载结束" ;
// 不变,通知发送时间
notify.when = System.currentTimeMillis() ;
// 不变,设置通知到达时声音,闪亮等效果
notify.defaults = Notification.DEFAULT_ALL ;
// 不变,设置通知看过之后销毁
notify.flags = Notification.FLAG_AUTO_CANCEL ;
// 设置通知滑下后显示的标题,内容以及点击时要执行的操作
Intent aIntent = new Intent(DemoService.this , DownOverActivity.class) ;
Log.v("DownService , notify" , url) ;
aIntent.putExtra("url", url) ;
// Notification中启动Activity必须添加该语句
aIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK) ;
PendingIntent contentIntent = PendingIntent.getActivity(
DemoService.this,
flag, // 该参数是PendingIntent的标识id,如果该id之前已存在再次使用时会覆盖之前的PendingIntent
aIntent,
PendingIntent.FLAG_UPDATE_CURRENT
) ;
notify.setLatestEventInfo(
DemoService.this,
"下载已经结束",
url + "|over",
contentIntent
) ;
// 发送通知
NotificationManager notifyM = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE) ;
notifyM.notify(flag, notify) ;
}
}.start() ;
}
// Service 被销毁时调用
@Override
public void onDestroy() {
Log.v("Service", "onDestroy") ;
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
}