简体   繁体   中英

I created a service in Java but it doesn't run in the background

I am trying to create a simple service in java that will run in the background. I want to play a song on the phone even though the app is closed. I read some articles and watched tutorials but my service still stops when I close the app.

1) I changed my manifest

<service
            android:name="com.example.myapplication.ServiceSound"
            android:exported="true"
            android:enabled="true">
        </service>

2) I returned START_STICKY

This is manifest

<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        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>

        <service
            android:name="com.example.myapplication.ServiceSound"
            android:exported="true"
            android:enabled="true">
        </service>
    </application>

This is the service

public class ServiceSound extends Service {
    MediaPlayer player;

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        player = MediaPlayer.create(this, Settings.System.DEFAULT_RINGTONE_URI);
        player.start();
        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        player.stop();
    }
}

And this is how I start the service in MainActivity.java

startService(new Intent(getBaseContext(),ServiceSound.class));

I think you need Broadcast Receiver. So now 1- Create broadcast receiver class and call your Service class.

  public class MyBroadcaseReceiver extends BroadcastReceiver {

   @Override
   public void onReceive(Context context, Intent intent) {
       context.startService(new Intent(context,ServiceSound.class));
       Log.e(TAG, "onReceive: BroadCast is started");
   }
}

2- Your Service class define this

       public static String str_receiver = "Your_Package_Name.receiver";

on startCommand start your broadcast class

   public int onStartCommand(Intent intent, int flags, int startId) {
        player = MediaPlayer.create(this, Settings.System.DEFAULT_RINGTONE_URI);
        player.start();
        return START_STICKY;
        Intent intent1 = new Intent(this,MyBroadcaseReceiver.class);
        sendBroadcast(intent1);
        return START_STICKY;


       }

Now in your MainActivity class create an object for broadcast private BroadcastReceiver broadcastReceiver;

inside onCreate() define the object broadcastReceiver = new MyBroadcaseReceiver();

Finaly call onResume() and onPause() inside the MainActivity.

   @Override
    protected void onResume() {
        super.onResume();
        registerReceiver(broadcastReceiver, new IntentFilter(MyService.str_receiver));
    }
   @Override
    protected void onPause() {
        super.onPause();
        unregisterReceiver(broadcastReceiver);

    }

Don't forget to call broadcast reciever in your Manifest file

           <receiver android:name=".MyBroadcaseReceiver"></receiver>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM