1

好吧,我正在关注此链接

甚至添加了广播接收器,如上面的链接所示。但我没有通知任务完成,我想在这个 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 服务完成后运行一些活动。任何人都可以帮助我。

4

2 回答 2

5

步骤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 步: 扩展 BroadcastReceiver 的 MainActivity.class 中的子类。

    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();
    }

}

第 3 步: 在 IntentService 中:

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

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

于 2013-07-02T11:54:41.287 回答
1

在我的主要活动中,我定义了一个状态标志

private final Handler LoginHandler = new Handler() {
        @Override
        public void handleMessage(Message loginmessage) {
            Bundle receivedData = loginmessage.getData();
            loginStatus = receivedData.getString("status");

                   if(loginStatus.equals("done")){
                     //do something
                   }else{
                     //do something
                    }
                 };
            };

在意图服务中,如果工作完成,我发送一个值“done”,如果有任何错误,则发送一个“connectionerror”

try {


            if (LoginBundle != null) {

                Messenger LoginMessenger = (Messenger) LoginBundle
                        .get("LOGINMESSENGER");
                Message LoginMessage = Message.obtain();

                LoginBundle.putString("status", "Done");

                try {
                    LoginMessenger.send(LoginMessage);
                }

                catch (android.os.RemoteException e1) {
                    Log.w("Message Not Sent", e1);
                }
            }

        } catch (JSONException e) {

            Bundle LoginBundle = intent.getExtras();
            Message LoginMessage = Message.obtain();
            Messenger LoginMessenger = (Messenger) LoginBundle
                    .get("LOGINMESSENGER");
            LoginBundle.putString("status", "Connection Error");
            LoginMessage.setData(LoginBundle);

            try {
                LoginMessenger.send(LoginMessage);
            }

            catch (android.os.RemoteException e1) {
                Log.w("Message Not Sent", e1);
            }

        } catch (Exception ex) {
            Log.w("Unhandled", ex);
        }

所以在主要活动中,如果我得到任何回复,我可以检测到意图服务已完成其工作。

希望您理解我识别服务状态的方式。

于 2013-07-02T10:10:21.153 回答