Good day!
I would be very grateful if you help me find the error in my code.
There are two applications.
Appendix 1 has two buttons that start and stop the service from Appendix 2.
Application1 does not have an Activity. It has only a Java class with the service.
Application1 starts up, but an error occurs when starting one of the buttons.
If I understand correctly, the problem with Intent.
I would really appreciate any replies comments on the topic.

Appendix 1 (Manifest)

<application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> 

Appendix 1 (Activity)

 public class MainActivity extends AppCompatActivity { Intent intent; @Override protected void onCreate(Bundle savedInstanceState) { setContentView(R.layout.activity_main); intent = new Intent("com.example.timbook.myservice.MService"); } public void onClickStart (View v) { startService(intent); } public void onClickStop (View v) { stopService(intent); } } 

Appendix 2 (Manifest)

 <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <service android:name="com.example.timbook.myservice.MService" android:enabled="true" android:exported="true"> <intent-filter> <action android:name="com.example.timbook.myservice.MService"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> </service> 

Appendix 2 (Service)

 public class MService extends Service{ MediaPlayer mp; @Nullable @Override public IBinder onBind(Intent intent) { return null; } @Override public void onCreate() { super.onCreate(); mp = MediaPlayer.create(this,R.raw.glassanimals); mp.start(); } } 
  • When you launch the second application yourself, does it start without errors? And what's stopping you from launching service in application 1 and not doing a separate application? And in the end - where is the error log? - Vladimir
  • Vladimir, thanks for the comment. I am taking courses and the task was set to "Use the service from a separate application." In the second application there should be only a Service and it should not have an Activity. The second application does not start because it does not have an Activity. - Tim

2 answers 2

If you want to do without Activation at all, then the application needs to be awakened by sending Broadcast intent with the FLAG_INCLUDE_STOPPED_PACKAGES flag.

Read here: http://ashimita.blogspot.com.by/2012/04/broadcast-receiver-change-in-flow-since.html?m=1

You can also wake up the application via startService by Intent.

  • Vladimir, if I understood you correctly, I used the second version of startService that you proposed. In Activity Applications 1, it is launched via onClick. At the same time, the application closes immediately when you click one of the buttons. - Tim
  • @Tim we start Service from application 2 like this: Intent intent = new Intent ("action name"); this.startService (intent); - Vladimir
  • And in application 1, in the manifest, add the intent filter to the <service> block: android: name = ". StartClient" android: enabled = "true" android: exported = "true"> <intent-filter> <action android: name = "action name"> </ action> </ intent-filter> - Vladimir
  • Unfortunately, not one of the suggested options did not solve the program error. After I find the error, I will leave a message indicating the error in the code. - Tim

The problem was that it was not possible to link the two projects. The solution is as follows:

 component = new ComponentName("com.example.macbook.project", "com.example.macbook.project.MyService"); explicitIntent = new Intent("com.example.macbook.project.MyService"); explicitIntent.setComponent(component); 

ComponentName firstly contains the address of the project and, comma-separated, the full path to the Service .