I would be very grateful if you help me to understand where I made a mistake in the code.
I have two applications.
App1 has activity with buttons which start and stop the service from App2.
App2 has no activity. It has only Java class with Service.
I will be very thankful for any comments and answers.
Thanks!
App1 (Activity):
public class MainActivity extends AppCompatActivity {
Intent intent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(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);
}
}
App1 (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>
</application>
App2 (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();
}
}
App2 (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>
</application>
Aucun commentaire:
Enregistrer un commentaire