【问题标题】:How do I sent an Intent from one of my Apps to another of my Apps?如何将 Intent 从我的一个应用程序发送到另一个应用程序?
【发布时间】:2018-06-24 17:25:25
【问题描述】:

我有一个课程IntentService,在com.mypackage.app1.receiver(第一个应用程序)中:

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 接收。我该怎么做才能收到它?

【问题讨论】:

    标签: android android-intent android-service


    【解决方案1】:

    替换:

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

    将您正在使用的IntentstartService() 匹配。

    另外,由于有一个名为 IntentService 的 Android SDK 类,我建议您将服务名称更改为其他名称。

    【讨论】:

      猜你喜欢
      • 2016-02-03
      • 1970-01-01
      • 2014-11-01
      • 1970-01-01
      • 2017-01-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多