Access my own Service methods from Activity
I have a method in a service that I created, and I want to access this method from an Activity that implements a Prog开发者_开发百科ress Dialog. This method simply update my database, and it was returning an ANR problem, so I created a Thread and in this thread I want to call this method that is in my Service. I tried instantiating the Service, but the object is null
So, how to create an 'object' in my activity where I can access this method. Someone could help me with that implementation??
Thanks.
The code:
public class UpdateDBProgressDialog extends Activity {
private String LOG_TAG = "UpdateDBProgressDialog";
private TextView tv;
private ProgressDialog pd;
private Handler handler;
private RatedCallsService rcs;
private Intent intent;
public boolean mIsBound = false;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
pd = ProgressDialog.show(this, "Updating Database", "The application is updating the database. Please wait.", true, false);
handler = new Handler() {
@Override
public void handleMessage(Message msg) {
pd.dismiss();
}
};
Thread thread = new Thread(){
public void run() {
try{
rcs.updateDB();// Here I'm trying to call the method that is from the service class. But it says 'rcs' is null.
handler.sendEmptyMessage(0);
}catch(Exception e){e.printStackTrace();}
}
};
thread.start();
new Thread(){
public void run(){
try{
Thread.sleep(10000);
if(!RatedCallsService.RUNNING){
Intent i = new Intent(UpdateDBProgressDialog.this, RatedCallsService.class);
UpdateDBProgressDialog.this.startService(i);
}
}catch(Exception e){e.printStackTrace();}
}}.start();
}
}
I just want an object of the service so I can call the method I created there.
When you start or bind a Service its onCreate method calls and whenever you Start/Bind it again, its onStartCommand method will call. Because service is already created.
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
String action = intent.getAction();
if (action.equals(ACTION_PLAY)) // where ACTION_PLAY="any.unique.name.play"
processPlayRequest();
else if (action.equals(ACTION_PAUSE)) // where ACTION_PAUSE="any.unique.name.pause"
processPauseRequest();
else if (action.equals(ACTION_SKIP))
processSkipRequest();
else if (action.equals(ACTION_STOP))
processStopRequest();
else if (action.equals(ACTION_REWIND))
processRewindRequest();
else if (action.equals(ACTION_URL))
processAddRequest(intent);
return START_NOT_STICKY; // Means we started the service, but don't want
// it to
// restart in case it's killed.
}
And call it like:
Intent serviceIntent = new Intent(this, ServiceToStart.class);
serviceIntent.setAction("any.unique.name.play"); // onClick of play button
startService(serviceIntent);
If you want to invoke an operation on a Service from an Activity, there are two options. One is to create an Intent with any data set as extras on the Intent, and then start the Service kind of like you have done in the code. Then in the Service's onStartCommand, pull out the extras and invoke the appropriate method on your Service. For this option, you probably want to use an IntentService. The other option is to bind to the Service and directly invoke methods on it. To do this you will need to declare your Service's operations using AIDL. See the API documentation on Service for details.
精彩评论