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(); } }