How to pause background music service when you exit/pause the application? - java

I want to have background music playing while the user is playing a game. The music starts when the user starts the application, pauses when they leave it, and it resumes when they go back to the application.
I tried using this method, I edited it a bit:
public class MainActivity extends Activity {
private boolean bounded;
private BackgroundSoundService backgroundSoundService;
ServiceConnection connection = new ServiceConnection() {
#Override
public void onServiceDisconnected( ComponentName name ) {
bounded = false;
backgroundSoundService = null;
}
#Override
public void onServiceConnected( ComponentName name, IBinder service ) {
bounded = true;
BackgroundSoundService.LocalBinder localBinder = (BackgroundSoundService.LocalBinder) service;
backgroundSoundService = localBinder.getServiceInstance();
}
};
#Override
public void onCreate( Bundle savedInstanceState ) {
super.onCreate(savedInstanceState);
// (code that's not necessary)
backgroundSoundService.start(); // this is where the error is thrown
}
#Override
public void onPause() {
super.onPause();
backgroundSoundService.pause();
}
#Override
public void onResume() {
super.onResume();
backgroundSoundService.resume();
}
#Override
public void onStop() {
super.onStop();
backgroundSoundService.pause();
}
#Override
public void onStart() {
super.onStart();
Intent intent = new Intent(this, BackgroundSoundService.class);
bindService(intent, connection, BIND_AUTO_CREATE);
backgroundSoundService.start();
}
#Override
public void onDestroy() {
super.onDestroy();
backgroundSoundService.destroy();
}
}
I use an activity to play, pause and resume background music. I'll leave out the unecessary methods/lines for this question here:
public class BackgroundSoundService extends Service {
private static final String TAG = null;
public IBinder binder = new LocalBinder();
public IBinder onBind( Intent arg0 ) {
return binder;
}
public IBinder onUnBind( Intent arg0 ) {
return null;
}
public class LocalBinder extends Binder {
public BackgroundSoundService getServiceInstance() {
return BackgroundSoundService.this;
}
}
}
However, when I run the application I get a NullPointerException in the MainActivity class (in the onCreate method, I commented it in the code).
The variable doesn't seem to be initialized yet, but I do need to start the music when the user opens the application.
I also tried removing the backgroundSoundService.start(); from the onCreate method, so the music would start when onStart is called. However, when I do that, I get the same error.
So, how can I initialize backgroundSoundService before it is used to call its methods?

first of all remove this backgroundSoundService.start() from onCreate and add it inside onServiceConnected() method
u need to check null before doing any backgroundSoundService related stuffs like below
#Override
public void onPause() {
super.onPause();
if(backgroundSoundService != null){
backgroundSoundService.pause();
}
}
add this kind of null check in all appearance of backgroundSoundService

Related

onResume() and onPause() is being called on the first launch

I am having an activity and on Launching the activity through Intent from one of my service, onCreate(), onPause() and onResume() is called.
I am not very sure where i am doing wrong, i have removed all the code, just the basic function codes are available, and also i have removed all the codes which were opening that particular activity, still the behaviour is same.
I am including the simplest code i am using
ActivityClass.java
public class ActivityClass extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
System.out.println("TEST : onCreate");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lockscreen);
ButterKnife.bind(this);
}
#Override
protected void onPause() {
System.out.println("TEST : onPause");
super.onPause();
}
#Override
protected void onDestroy() {
System.out.println("TEST : onDestroy");
super.onDestroy();
}
}
Service class
public class OverlayService extends Service {
Context context;
public static final String TAG = OverlayService.class.getSimpleName();
public OverlayService(Context applicationContext) {
super();
context = applicationContext;
}
public OverlayService() {
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG, "[onCreateService]");
super.onStartCommand(intent, flags, startId);
KeyguardManager myKM = (KeyguardManager) getApplicationContext().getSystemService(Context.KEYGUARD_SERVICE);
if(myKM.inKeyguardRestrictedInputMode()) {
//it is locked
showOverlayActivity();
} else {
//it is not locked
}
registerOverlayReceiver();
context = this;
return START_STICKY;
}
#Override
public void onDestroy() {
super.onDestroy();
unregisterOverlayReceiver();
Log.i("EXIT", "ondestroy!");
}
#Override
public void onTaskRemoved(Intent rootIntent) {
super.onTaskRemoved(rootIntent);
Intent broadcastIntent = new Intent("ac.in.ActivityRecognition.RestartSensor");
sendBroadcast(broadcastIntent);
}
#Nullable
#Override
public IBinder onBind(Intent intent) {
return null;
}
private void unregisterOverlayReceiver() {
if (overlayReceiver != null) {
unregisterReceiver(overlayReceiver);
}
}
private static final String ACTION_DEBUG = "kunal.lockoverlay.action.DEBUG";
private void registerOverlayReceiver() {
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_SCREEN_ON);
filter.addAction(ACTION_DEBUG);
registerReceiver(overlayReceiver, filter);
}
private BroadcastReceiver overlayReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Log.d(TAG, "[onReceive]" + action);
if (action.equals(Intent.ACTION_SCREEN_ON)) {
// ACTON_SCREEN_ON
showOverlayActivity();
} else if (action.equals(ACTION_DEBUG)) {
showOverlayActivity();
}
}
};
private void showOverlayActivity() {
Intent intent = new Intent();
intent.setClass(this, ActivityClass.class);
intent.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
}
Can someone point some of the possible reasons for this behaviour or could identify where i am doing wrong?
Well everything will be called regardless, you have defined it in your ClassName.java file.
You override the method and define it in your class to perform certain action/function when that is called.
For eg.,
You could use onResume to clear an arraylist and add updated elements to arraylist (OR) dismiss all notifications from your app on moving to that activity, say
#Override
public void onResume() {
super.onResume();
calendarList.clear();
//dismiss all notifications here
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
if (notificationManager!=null)
notificationManager.cancelAll();
}
OR eg., onDestroy to destroy the bluetooth service,
#Override
public void onDestroy() {
super.onDestroy();
if (mBluetoothService != null) {
mBluetoothService.stop();
}
}
Hope that clarifies. Happy coding!
About service class, you should call registerReceiver() or initialize variables in onCreate instead of onStartCommand because onCreate is only called once at the first time of starting service and onStartCommand will be called every time you want to trigger an action to the service by calling startService(Intent).

Background service isn't working properly

I'm trying to implement service in android to make an app locker.
I'm trying to check the which activity is running on the foreground and if it's locked, forwarding it to my Locker activity.
I've added the service in manifest too, but it isn't working at all.
Here's the code `
private static Timer timer = new Timer();
public Boolean userAuth = false;
private Context mContext;
public String pActivity = "";
public IBinder onBind(Intent arg0) {
return null;
}
public void onCreate() {
super.onCreate();
mContext = this;
startService();
}
private void startService() {
timer.scheduleAtFixedRate(new mainTask(), 0, 500);
}
private class mainTask extends TimerTask {
public void run() {
toastHandler.sendEmptyMessage(0);
}
}
public void onDestroy() {
super.onDestroy();
Toast.makeText(this, "Service Stopped ...", Toast.LENGTH_SHORT).show();
}
private final Handler toastHandler = new Handler() {
#Override
public void handleMessage(Message msg) {
String activityOnTop;
ActivityManager manager = (ActivityManager)mContext.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> tasks = manager.getRunningAppProcesses();
//Getting the foreground activity name
activityOnTop=tasks.get(0).processName;
//Checking it against the app I need to lock
if (activityOnTop.equalsIgnoreCase("com.droiddigger.techmanik")) {
Intent lockIntent = new Intent(mContext, Lockscreen.class);
lockIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.startActivity(lockIntent);
} else if(activityOnTop.equalsIgnoreCase("com.droiddigger.applocker")){
}else{
}
}
};
You must start that service. It can be done in an Activity or a BroadcastReceiver.
startService(new Intent(this, UpdaterServiceManager.class));
For example:
public class MyActivity extends Activity {
#Override
public void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
startService(new Intent(this, YourService.class));
finish();
}
}
EDIT:
You are always retrieving the item 0 of the list called tasks. Looking at the SDK documentation, it is said that list order is not especified: https://developer.android.com/reference/android/app/ActivityManager.html#getRunningAppProcesses()
Returns a list of RunningAppProcessInfo records, or null if there are
no running processes (it will not return an empty list). This list
ordering is not specified.
You must get the current visible activity other way. I suggest an AccessibilityService

Android - Service doesn't create

I have some programm, where I want to save the instance of service and use it in another activities. But service doesn't create.
Just see the code, please.
MainActivity.java:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MainService.get(this);
}
}
MainService.java:
public class MainService extends Service {
public static Object sWait = new Object();
public static MainService instance;
#Override
public IBinder onBind(Intent intent) {
return null;
}
public static MainService get(Context mContext) {
if (instance == null) {
Intent intent = new Intent(mContext, MainService.class);
mContext.startService(intent);
}
while (true) {
if (instance != null) {
Log.v("myLogs", "all is good!");
break;
}
synchronized (sWait) {
try {
sWait.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
return instance;
}
#Override
public void onCreate() {
Log.v("myLogs", "created!");
instance = this;
synchronized (sWait) {
sWait.notify();
}
}
}
When I don't use while in MainService.get(), service creates. I added this service to manifest file. I don't know where is there error. I need your help.
That's just not the way you should use Service in Android.
What you probably need is to use bindService() call from your Activity.
See more about bound service here http://developer.android.com/guide/components/bound-services.html
If you need the service to be the same instance each time you bind to it, call startService once when your application start.
Example code:
public class LocalBinder<T> extends Binder {
private WeakReference<T> mService;
public LocalBinder(T service) {
mService = new WeakReference<T>(service);
}
public T getService() {
return mService.get();
}
}
public class MyService extends Service {
private final LocalBinder<MyService> binder;
public MyService() {
binder = new LocalBinder<MyService>(this);
}
#Override
public IBinder onBind(Intent intent) {
return binder;
}
}
public class MyActivity extends Activity implements ServiceConnection {
#Override
protected void onStart() {
super.onStart();
bindService(new Intent(this, MyService.class), this, Context.BIND_AUTO_CREATE);
}
#Override
protected void onStop() {
super.onStop();
unbindService(this);
}
#SuppressWarnings("unchecked")
#Override
public void onServiceConnected(ComponentName name, IBinder service) {
MyService serv = ((LocalBinder<MyService>) service).getService();
// serv is your service instance now
}
#Override
public void onServiceDisconnected(ComponentName name) {
}
}
To start your service use (call this from your activity!):
Intent serviceIntent = new Intent(this, MainService.class);
startService(serviceIntent);
see docs: https://developer.android.com/training/run-background-service/send-request.html
That is definitely not the way you are supposed to use services in android.
I would suggest you revisit the offical android docs.
To get a quick introduction visit this tutorial here.

Onchange of content observer not getting called upon bootup

i am developing an android app, where i am registering my ContentObserver class to detect
the changes in VOLUME_RING.
My problem is, i am calling the ContentObserver class upon bootup of the phone, and i am able to get inside the Contentobserver class, but the onchange method is not getting called(i.e, the changes in volume is not getting detected).
Please check out the code below,
//BootupReceiver code.
public class Bootupreceiver extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
Intent startServiceIntent = new Intent(context,ServiceforVolumeChecker.class);
startServiceIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startService(startServiceIntent);
}
}
//code to register the Content observer class.
public class ServiceforVolumeChecker extends Service
{
private VolumeChecker Volume;
#Override
public void onCreate()
{
super.onCreate();
Log.e("Service","Service");
Volume = new VolumeChecker(this,new Handler());
String vol_ring = android.provider.Settings.System.VOLUME_RING;
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
if (currentapiVersion >= 17)
{
getApplicationContext().getContentResolver().registerContentObserver(android.provider.Settings.System.CONTENT_URI, true, Volume );
}
else
{
getApplicationContext().getContentResolver().registerContentObserver(android.provider.Settings.System.getUriFor(vol_ring), true, Volume);
}
stopService(new Intent(this, ServiceforVolumeChecker.class));
}
#Override
public IBinder onBind(Intent intent)
{
// TODO Auto-generated method stub
return null;
}
//code of ContentObserver class.
public class VolumeChecker extends ContentObserver
{
public VolumeChecker(Context c, Handler handler)
{
super(handler);
context=c;
this.handler = handler;
}
#Override
public boolean deliverSelfNotifications()
{
return super.deliverSelfNotifications();
}
#Override
public void onChange(boolean selfChange)
{
super.onChange(selfChange);
}
Please help! Thanks!
Put the service task in onStartCommand instead of using onCreate

Android Bind Service returns false every time

boolean isBound = bindService(new Intent(SocketServiceController.this, SocketService.class), mConnection, Context.BIND_AUTO_CREATE);
Bind service always returns false for me... Could anyone tell me the possible errors that i could have made...
Service code is as follows
public class SocketService extends Service{
#Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return myBinder;
}
private final IBinder myBinder = new LocalBinder();
public class LocalBinder extends Binder {
public SocketService getService() {
return SocketService.this;
}
}
#Override
public void onCreate() {
super.onCreate();
}
public void IsBoundable(){
Toast.makeText(this,"Is bound", Toast.LENGTH_LONG).show();
}
public void onStart(Intent intent, int startId){
super.onStart(intent, startId);
Toast.makeText(this,"Service created ...", Toast.LENGTH_LONG).show();
}
#Override
public void onDestroy() {
super.onDestroy();
}
}
Service Controller code is as follows:
public class SocketServiceController extends Activity{
private SocketService mBoundService;
private Boolean mIsBound;
public SocketServiceController ssc;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ssc = this;
setContentView(R.layout.telnet);
Button startButton = (Button)findViewById(R.id.button1);
Button endButton = (Button)findViewById(R.id.button2);
Button bindButton = (Button)findViewById(R.id.button3);
startButton.setOnClickListener(startListener);
endButton.setOnClickListener(stopListener);
//bindButton.setOnClickListener(this);
TextView textView = (TextView)findViewById(R.id.textView1);
}
private ServiceConnection mConnection = new ServiceConnection() {
#Override
public void onServiceConnected(ComponentName className, IBinder service) {
mBoundService = ((SocketService.LocalBinder)service).getService();
}
#Override
public void onServiceDisconnected(ComponentName className) {
mBoundService = null;
}
};
private void doBindService() {
boolean isBound = bindService(new Intent(SocketServiceController.this, SocketService.class), mConnection, Context.BIND_AUTO_CREATE);
mIsBound = true;
//mBoundService.IsBoundable();
}
private void doUnbindService() {
if (mIsBound) {
// Detach our existing connection.
unbindService(mConnection);
mIsBound = false;
}
}
private OnClickListener startListener = new OnClickListener() {
public void onClick(View v){
startService(new Intent(SocketServiceController.this,SocketService.class));
doBindService();
}
};
private OnClickListener stopListener = new OnClickListener() {
public void onClick(View v){
stopService(new Intent(SocketServiceController.this,SocketService.class));
}
};
#Override
protected void onDestroy() {
super.onDestroy();
doUnbindService();
}
}
I had the same problem. After a time of studying, I found out that our application does not know which service to be bound. This is because either we didn't declare the service in the manifest file, or we declared it in the wrong way.
In my case, I declare it as:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="vn.abc"
.................
<service android:name=".SocketService" >
</service>
By this way, Android will understand that the service has the package as vn.abc.SocketService, but in fact, in my code structure, my service has the package com.tung.SocketService (packages here are just examples). That is the reason why Android can not find the service I declared in the manifest file.
One very common case in which bindService() returns false is if the service was not declared in the Manifest. In that case you should declare your service in manifest file.
<manifest ... >
...
<application ... >
<service android:name=".MyService" />
...
</application>
</manifest>
I think the problem might be while binding the service.I m using the following code to bind the service.Its returning true properly.
boolean flag=bindService(mService, mConnection, MODE_PRIVATE);
mService -is the service object,
mConnection- is serviceConnection object
Mode
There might be a small change in your code
boolean isBound = bindService(mBoundService, mConnection, Context.BIND_AUTO_CREATE);
It might work..
Have a great day...
I had a similar error. It turned out to be due to the difference between these two blocks of code:
#Override
public IBinder onBind(Intent arg0)
{
return new MyBinder();
}
and:
private final IBinder mBinder = new MyBinder();
#Override
public IBinder onBind(Intent arg0)
{
return mBinder;
}
The first block doesn't work, the second block does.
I hope this helps save someone else the hours it took me to track this down.
I solved the problem doing a new project and copying the code in it. Probably the problem was related to package name.
Strangely on Android Studio I had the following situation (project not working):
name of the package: com.company.proj.server
name of the app: speedtest
name of the server: syncserver
This strangely blocked the server to be seen outside the app
In the new project I had (working):
name of the package: com.company.proj.server.speedtest
name of the app: speedtest
name of the server: syncserver
notice how the name of the app is the last element of the package.
The first situation allowed the app to be executed correctly, to send messages to the service but not to access the service from a different app (the flag android:exported="true" was correctly set in both cases)
i think the bound service flag is setting wrong.
you should set the flag in you service connection .
private ServiceConnection mServiceConnection = new ServiceConnection() {
#Override
public void onServiceDisconnected(ComponentName name) {
mServiceBound = false;
}
#Override
public void onServiceConnected(ComponentName name, IBinder service) {
BoundService.MyBinder myBinder = (BoundService.MyBinder) service;
mBoundService = myBinder.getService();
mServiceBound = true;
}
};
i had done a simple exampl in github .
https://github.com/wingsum93/Bind_Service_Example
I had the same error and the reason was that I forgot to start the service.

Categories

Resources