abd*_*him 3 android broadcast android-pendingintent localbroadcastmanager
我在IntentService中运行后台任务.我在通知栏中显示进度(使用NotificationManager),如果有人点击该通知,则要取消该任务.
我用广播服务成功实现了它.但是,我在想是否有可能对localBroadcast做同样的事情.
如果我将"registerReceiver"更改为"LocalBroadcastManager.getInstance(this).registerReceiver",则代码不起作用.你能否告诉我为什么我不能使用localbroadcast和pendingintent?
谢谢,
目前我的代码是:
boolean isCancelled;
Context context;
NotificationManager mNotifyManager;
NotificationCompat.Builder mBuilder;
int mId = 2;
protected void onHandleIntent(Intent intent) {
isCancelled = false;
context = getApplicationContext();
IntentFilter filter = new IntentFilter();
filter.addAction("com.migrationdesk.mylibman.MyServiceClass.STOP");
registerReceiver(receiver, filter);
getNotification();
mainTask(); // THIS IS SOME BACKGROUND TASK
}
private final BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(action.equals("com.migrationdesk.mylibman.MyServiceClass.STOP")){
isCancelled = true;
}
}
};
protected void getNotification() {
Intent newintent = new Intent();
newintent.setAction("com.migrationdesk.mylibman.MyServiceClass.STOP");
PendingIntent pIntent = PendingIntent.getBroadcast(context, 0, newintent, 0);
mNotifyManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mBuilder = new NotificationCompat.Builder(context);
mBuilder.setContentTitle("Cover Download")
.setContentText("In progress")
.setContentIntent(pIntent)
.setSmallIcon(R.drawable.ic_launcher);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1903 次 |
| 最近记录: |