1

さて、私はこのリンクをたどっています

上記のリンクに示すように、ブロードキャストレシーバーも追加しました。しかし、私はタスクの完了について通知していません。このインテント サービスの完了後に何らかのアクティビティを実行したいと考えています。誰でも私を助けてくれますか?

次のようなエラーが表示されます:

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

インテントサービスで私は初期化しました:

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>

上記のリンクですぐにブロードキャストレシーバーを追加しました。しかし、私はタスクの完了について通知していません。このインテント サービスの完了後に何らかのアクティビティを実行したいと考えています。誰でも私を助けてくれますか?

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 に答える