簡體   English   中英

如何從一個應用程序向另一個應用程序發送意圖?

[英]How do I sent an Intent from one of my Apps to another of my Apps?

我在com.mypackage.app1.receiver (第一個應用程序)中有一個IntentService類:

public class IntentService extends Service {
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.w("INTENT SERVICE", "Received request to start intent");
        return super.onStartCommand(intent, flags, startId);
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        Log.w(TAG, "onBind");
        return null;
    }
}

我在清單中注冊:

<service android:name=".IntentService" >
    <intent-filter>
        <action android:name="com.mypackage.START_INTENT"/>
        <category android:name="com.mypackage.category.DEFAULT"/>
        <data android:mimeType="text/plain"/>
    </intent-filter>
</service>

在另一個應用程序中,我在com.mypackage.app2.sender中具有SenderActivity ,只是應該在打開並立即關閉時開始發送意圖:

public class SenderActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent intent = new Intent("com.learningleaflets.START_INTENT");
        intent.setPackage("com.mypackage");
        startService(intent);
        finish();
    }
}

不幸的是,目前的意向SenderActivity將不會被收到IntentService 我該怎么做才能得到它?

更換:

<service android:name=".IntentService" >
    <intent-filter>
        <action android:name="com.mypackage.START_INTENT"/>
        <category android:name="com.mypackage.category.DEFAULT"/>
        <data android:mimeType="text/plain"/>
    </intent-filter>
</service>

與:

<service android:name=".IntentService" >
    <intent-filter>
        <action android:name="com.learningleaflets.START_INTENT"/>
    </intent-filter>
</service>

以匹配您與startService()一起使用的Intent

另外,由於存在一個名為IntentService的Android SDK類,因此建議您將服務名稱更改為其他名稱。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM