Hi,
I am new to world of Android Development and experimenting on Isolation of services in Android. For the purpose, I created an application that play a music file on button press. Whenever i am trying to put isolation flag to true. The application flag crashes
android:isolatedProcess="true". My application crashes. Following is the code. Can anyone help any corrections.
Thanks in advance.
I am new to world of Android Development and experimenting on Isolation of services in Android. For the purpose, I created an application that play a music file on button press. Whenever i am trying to put isolation flag to true. The application flag crashes
android:isolatedProcess="true". My application crashes. Following is the code. Can anyone help any corrections.
Thanks in advance.
Code:
Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.o7planning.playsongservice">
<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>
<service
android:name=".PlaySongService"
android:enabled="true"
android:isolatedProcess="true"
android:exported="true"></service>
</application>
</manifest>
Code:
MainActivity
package org.o7planning.playsongservice;
import android.app.Activity;
import android.content.Context;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.os.IBinder;
import android.widget.Toast;
import android.view.View;
public class MainActivity extends AppCompatActivity {
PlaySongService localService;
private boolean isBound = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
// This method is called when users click on the Start button.
public void playSong(View view) {
// Create Intent object for PlaySongService.
Intent myIntent = new Intent(MainActivity.this, PlaySongService.class);
bindService(myIntent, connection, Context.BIND_AUTO_CREATE);
// Call startService with Intent parameter.
if (isBound) {
this.startService(myIntent);
// myIntent.putExtra("pass","Activity is communicating with service");
}
}
private ServiceConnection connection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName className, IBinder service) {
PlaySongService.LocalBinder binder = (PlaySongService.LocalBinder) service;
localService = binder.getService();
isBound = true;
}
@Override
public void onServiceDisconnected(ComponentName arg0) {
isBound = false;
}
};
}
// This method is called when users click on the Stop button.
// public void stopSong(View view) {
// Create Intent object
// Intent myIntent = new Intent(MainActivity.this, PlaySongService.class);
// this.stopService(myIntent);
//}
//}
Code:PlaySongService.java package org.o7planning.playsongservice; import android.app.Service; import android.content.Intent; import android.media.MediaPlayer; import android.os.Bundle; import android.os.Binder; import android.os.IBinder; import android.widget.Toast; public class PlaySongService extends Service { private final IBinder binder = new LocalBinder(); public class LocalBinder extends Binder { PlaySongService getService() { return PlaySongService.this; } } private MediaPlayer mediaPlayer; public PlaySongService() { } @Override public void onCreate(){ super.onCreate(); // Create MediaPlayer object, to play your song. mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.mysong); } @Override public IBinder onBind(Intent intent) { return binder; } public int onStartCommand(Intent intent, int flags, int startId){ // Pass message from Activity. //Bundle extras = intent.getExtras(); // String from =(String) extras.get("pass"); // Toast.makeText(this, from, Toast.LENGTH_SHORT).show(); // Play song. mediaPlayer.start(); return START_STICKY; } // Destroy @Override public void onDestroy() { // Release the resources mediaPlayer.release(); super.onDestroy(); } }