Youn Kyu Lee
Youn Kyu Lee

Reputation: 123

Can start Service from another application using explicit Intent?

Can start Service from another application using 'explicit' Intent? It keeps showing error : not allowed to start service intent without permission How can I start Service of Receiver app?

Activity of Sending App:

    {

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

Upvotes: 2

Views: 3450

Answers (1)

shshnk
shshnk

Reputation: 1691

I was able to start the service using the method that you mentioned above. I think you need to run the service in a global process in your receiver app and also you need to export it. Here is what the service node of my manifest file looks like

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

In the activity of Sending app this is how I connect to the 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");

Upvotes: 3

Related Questions