2013-07-02 32 views
0

嗯,我跟着这link如何在完成任务后从intentservice获取通知?

甚至添加广播reciver如上面的链接所示。但我没有通知任务完成,我想在完成此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

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

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

在我发出“完成”,如果工作完成,“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); 
     } 
值的意图服务

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

希望你明白我的方式来确定服务的状态。

2

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

} 

第3步: 在IntentService:

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

此代码工作正常,我。 干杯。

+0

好,简单的说明。谢谢。 –

相关问题