我在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);}
解决方法:
However, I was thinking is it possible to do the same with localbroadcast.
这是不可能的. PendingIntent仅适用于常规广播,并且管理通知栏的代码无论如何都不在您的过程中.
总结以上是内存溢出为你收集整理的android – 使用PendingIntent进行全局广播或本地广播全部内容,希望文章能够帮你解决android – 使用PendingIntent进行全局广播或本地广播所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)