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.
Related
I want to start 2 services at the start of an activity but only the first one starts and the second fails at bindService(). There is no error just when I want to do something with the service it gives me a nullpointer. I also tried to wait to do something but the service never starts.
The 2 services are pretty similar and I just want to know what is wrong with the implementation. I tried to debug and bindservice() function at startSoundmanagerService return 0, what is maybe the root of the problem but I don't know why.
public class SimulationActivity extends AppCompatActivity {
BluetoothService BService;
boolean mBound = false;
SoundManager SService;
boolean sBound = false;
#Override
protected void onStart() {
super.onStart();
if (bluetoothAdapter.isEnabled()) {
setStatusText("Bluetooth on");
}
else {
setStatusText("Bluetooth off");
}
if(!mBound) {
startServer();
}
conStatImageView.setImageResource(R.drawable.connection_off);
if(!sBound){
startSoundManagerService();
}
#Override
protected void onStop() {
super.onStop();
if(mBound) {
unbindService(bConnection);
mBound = false;
}
if(sBound){
unbindService(sConnection);
sBound = false;
}
}
private ServiceConnection bConnection = new ServiceConnection() {
#Override
public void onServiceConnected(ComponentName className,
IBinder service) {
BluetoothService.LocalBinder binder =
(BluetoothService.LocalBinder) service;
BService = binder.getService();
mBound = true;
}
#Override
public void onServiceDisconnected(ComponentName arg0) {
mBound = false;
}
};
private ServiceConnection sConnection = new ServiceConnection() {
#Override
public void onServiceConnected(ComponentName className,
IBinder service) {
SoundManager.LocalBinder binder = (SoundManager.LocalBinder) service;
SService = binder.getService();
sBound = true;
}
#Override
public void onServiceDisconnected(ComponentName arg0) {
sBound = false;
}
public void startServer(){
if (bluetoothAdapter == null) {
Log.d("tag","Device doesn't support Bluetooth") ;
}
if (!bluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
if(bluetoothAdapter.isEnabled()){
startBluetoothservice();
}
}
private void startBluetoothservice(){
Intent intent = new Intent (this,BluetoothService.class);
bindService(intent, bConnection, Context.BIND_AUTO_CREATE);
Log.i(TAG,"Trying to start bluetoothservice");
}
private void startSoundManagerService(){
Intent intent = new Intent (this,SoundManager.class);
bindService(intent, sConnection, Context.BIND_AUTO_CREATE);
Log.i(TAG,"Trying to start soundservice");
}
So how can I implement 2 different services in 1 activity?
Edit Solution: I forgot to register the service in the manifestfile. ;)
I found the solution: I forgot to register the 2nd Service in the manifest. Thats all ;)
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
In my application I want to establish a TCP connection with a service and bind this service to every activity where it is needed. It gets started in my Login activity and everything is working like it should. Here is the code:
private TCPService mService;
private boolean mBound = false;
#Override
protected void onStart() {
super.onStart();
Intent intent = new Intent(this, TCPService.class);
startService(intent);
bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
}
#Override
protected void onStop() {
super.onStop();
if (mBound) {
unbindService(mConnection);
mBound = false;
}
}
/** Defines callbacks for service binding, passed to bindService() */
private ServiceConnection mConnection = new ServiceConnection() {
#Override
public void onServiceConnected(ComponentName className, IBinder service) {
// We've bound to LocalService, cast the IBinder and get LocalService instance
TCPService.LocalBinder binder = (TCPService.LocalBinder)service;
mService = binder.getService();
mBound = true;
}
#Override
public void onServiceDisconnected(ComponentName arg0) {
mBound = false;
}
};
Now I want to bind the service in another activity by using almost the same code just without startService(intent); But the mBound is never set to true and I can't access the service functions like it is intented.
Here is the implementation of the service:
public class TCPService extends Service {
private final IBinder myBinder = new LocalBinder();
public class LocalBinder extends Binder {
public TCPService getService() {
return TCPService.this;
}
}
#Override
public IBinder onBind(Intent intent) {
return myBinder;
}
}
What am I missing out? Is there anything I should check that may cause this behaviour?
Thanks in advance
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
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.