Binding service android
I am trying to bind an activity to a service and here is my code for that The below one is the code for activity
Button start = (Button) findViewById(R.id.button1);
Button stop = (Button) findViewById(R.id.button2);
start.setOnClickListener(this);
stop.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if(v.getId() == R.id.button1)
{
Intent i = new Intent(Intent.ACTION_MAIN);
i.setClassName("org.example","org.example.ServicesActivity");
bindService(i, conn, 0);
}
else if(v.getId() == R.id.button2)
{
unbindService(conn);
counter.setText("Number of Binding issss");
}
}
public ServiceConnection conn = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
System.out.println("Service is disconnected");
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
System.out.println("Service is connected");
}
};
and this is the code for my service
IBinder mBinder = new LocalBinder();
@Override
public IBinder onBind(Intent intent) {
System.out.println("came to onBind in service");
return mBinder ;
}
@Override
public void onCreate(){
super.onCreate();
System.out.println("came to oncreate in service");
}
@Override
public void onS开发者_开发技巧tart(Intent intent,int startId){
super.onStart(intent, startId);
System.out.println("came to onstart in service");
}
public class LocalBinder extends Binder{
ServicesActivity getService(){
System.out.println("came to Localbinder getservice in service");
return ServicesActivity.this;
}
}
My service and activity are two different apps My problem is that when i am pressing start button then the activity should bind to service but it not binding and it is not even showing any errors either can you plz tell me where i am doing error??? Thanks
Did you run an activity from within the apk that contains the Service since it was installed? You can't install a stand-alone service and expect it to run before there is user interaction. The user has to run it at least once to get it out of the "stopped" state. Then the service can react to intents. This security feature was introduced in Android 3.1.
http://developer.android.com/about/versions/android-3.1.html#launchcontrols
精彩评论