好吧,我正在关注这个link

甚至按上面的链接所示添加了广播接收器。但我没有通知任务已完成,我想在此Intent服务完成后进行一些活动。
谁能帮助我。

我收到如下错误:

Permission Denial: broadcasting Intent
{ act=com.saaranga.services.GetTaxonomy.DATA_RETRIEVED
cat=[android.intent.category.DEFAULT] (has extras) }
from com.saaranga.lib (pid=1089, uid=10034)
equires com.saaranga.services.Permissions.SEND_SIMPLE_NOTIFICATIONS
due to receiver com.saaranga.lib/com.saaranga.services.ServiceReciver


在intentService中,我已初始化:

public static final String ACTION_NOTIFY="com.saaranga.services.GetTaxonomy.DATA_RETRIEVED";

private void sendSimpleBroadcast(int count)
{
    Intent broadcastIntent = new Intent();

    broadcastIntent.setAction(GetTaxonomy.ACTION_NOTIFY);
    broadcastIntent.addCategory(Intent.CATEGORY_DEFAULT);
    broadcastIntent.putExtra(PARAM_OUT_COUNT, count);
//    broadcastIntent.setClass(getApplicationContext(), getClass());
    sendBroadcast(broadcastIntent, Permissions.SEND_SIMPLE_NOTIFICATIONS);
}


并创建一个权限类:

package com.saaranga.services;

public class Permissions {
public static final String SEND_SIMPLE_NOTIFICATIONS =  "com.saaranga.services.Permissions.SEND_SIMPLE_NOTIFICATIONS";}


并在清单文件中:

 <permission
    android:name="com.saaranga.services.Permissions.SEND_SIMPLE_NOTIFICATIONS"
    android:label="permission to send simple notifications"
    android:permissionGroup="android.permission-group.PERSONAL_INFO"
    android:protectionLevel="normal" />

<service
        android:name="com.saaranga.services.GetTaxonomy"
        android:enabled="true"
        android:exported="true">
</service>

<receiver android:name="com.saaranga.services.ServiceReciver"
              android:exported="true"
              android:permission="com.saaranga.services.Permissions.SEND_SIMPLE_NOTIFICATIONS">
         <intent-filter>
            <action android:name="com.saaranga.services.GetTaxonomy.DATA_RETRIEVED"/>
            <category android:name="android.intent.category.DEFAULT" />
         </intent-filter>
    </receiver>


甚至在上述链接中尽快添加了广播接收器。但我没有通知任务已完成,我想在此Intent服务完成后进行一些活动。
谁能帮助我。

最佳答案

步骤1:

在MainActivity.class中

private ResponseReceiver receiver;
//register reciver

IntentFilter filter = new IntentFilter(ResponseReceiver.ACTION_RESP);
    filter.addCategory(Intent.CATEGORY_DEFAULT);
    receiver = new ResponseReceiver();
    registerReceiver(receiver, filter);


第2步:
MainActivity.class中的子类,用于扩展BroadcastReceiver。

    public class ResponseReceiver extends BroadcastReceiver {
    public static final String ACTION_RESP = "com.saaranga.intent.action.MESSAGE_PROCESSED";
    @Override
    public void onReceive(Context context, Intent intent) {

        Toast.makeText(getApplicationContext(), "download complete", Toast.LENGTH_SHORT).show();
    }

}


第三步:
在IntentService中:

@Override
protected void onHandleIntent(Intent intent) {
Intent broadcastIntent = new Intent();
    broadcastIntent.setAction(ResponseReceiver.ACTION_RESP);
    broadcastIntent.addCategory(Intent.CATEGORY_DEFAULT);
    sendBroadcast(broadcastIntent);
}


这段代码对我来说很好用。
干杯。

关于android - 任务完成后如何从intentservice获得通知?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17422030/

10-12 01:46
查看更多