2013-07-02 47 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

好,簡單的說明。謝謝。 –

相關問題