可以使用显式Intent从另一个应用程序启动服务吗?

时间:2015-04-02 21:42:13

标签: android-intent

可以使用“显式”意图从其他应用程序启动服务吗? 它一直显示错误:not allowed to start service intent without permission 如何启动Receiver应用程序的Service

Activity发送应用:

    {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button button = (Button) findViewById(R.id.btn_call);
        button.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                ComponentName name =  new ComponentName("com.example.vic_app_1", "com.example.vic_app_1.SendSmsService");

                Intent abc = new Intent();
                abc.setComponent(name);
                abc.setAction(Intent.ACTION_MAIN);
                abc.addCategory(Intent.CATEGORY_LAUNCHER);
                abc.putExtra("destinationAddress", "5554");
                ComponentName c = getApplication().startService(abc);
                if (c == null) { Log.e("error", "failed to start with "+abc);         }

            }

Service Receiver App:

package com.example.vic_app_1;

public class SendSmsService extends Service {
{     @Override
      public IBinder onBind(Intent intent) {
        return null;
       }     
      @Override
      public void onStart(Intent intent, int startId) {
      Log.d("slog", "onStart()");
      super.onStart(intent, startId); 
      }

      @Override
      public void onDestroy() { 
      Log.d("slog", "onDestroy()");
      super.onDestroy();
      }
} }

Manifest Receiver App:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.vic_app_1"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="21" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name">
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <service android:name=".SendSmsService" >
    </service>
</application>

</manifest>

1 个答案:

答案 0 :(得分:3)

我能够使用您在上面提到的方法启动服务。我认为您需要在接收器应用程序中的全局流程中运行service,并且还需要将其导出。以下是service文件的manifest节点的样子

<service
        android:name="com.example.username.servicedemo.FirstService"
        <!--this will run your service in a global process-->
        android:process="com.example.username.servicedemo.remote"
        android:enabled="true"
        android:exported="true">
    </service>

在发送应用的activity中,这是我连接到service的方式

ComponentName componentName = new ComponentName("com.example.username.servicedemo","com.example.username.servicedemo.FirstService");
    Intent intent = new Intent();
    intent.setComponent(componentName);
    intent.putExtra("key","1234");