package com.example.hornsey.myapplication.IntentServiceDemo;
import android.app.IntentService;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import com.example.hornsey.myapplication.R;
public class ISDemoActivity extends AppCompatActivity {
private static final String ACTION_FOO = "com.example.hornsey.myapplication.IntentServiceDemo.action.FOO";
private static final String ACTION_BAZ = "com.example.hornsey.myapplication.IntentServiceDemo.action.BAZ";
private static final String EXTRA_PARAM1 = "Name";
private static final String EXTRA_PARAM2 = "Question";
private static final String EXTRA_PARAM3 = "Answer";
private static Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_isdemo);
context = ISDemoActivity.this;
findViewById(R.id.btn_action1).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActionFoo(context,"Jack","How are you?");
}
});
findViewById(R.id.btn_action2).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActionBaz(context,"Tom","Fine!");
}
});
}
/**
* Starts this service to perform action Foo with the given parameters. If
* the service is already performing a task this action will be queued.
*
* @see IntentService
*/
public static void startActionFoo(Context context, String param1, String param2) {
Intent intent = new Intent(context, MyIntentService.class);
intent.setAction(ACTION_FOO);
intent.putExtra(EXTRA_PARAM1, param1);
intent.putExtra(EXTRA_PARAM2, param2);
context.startService(intent);
}
/**
* Starts this service to perform action Baz with the given parameters. If
* the service is already performing a task this action will be queued.
*
* @see IntentService
*/
public static void startActionBaz(Context context, String param1, String param2) {
Intent intent = new Intent(context, MyIntentService.class);
intent.setAction(ACTION_BAZ);
intent.putExtra(EXTRA_PARAM1, param1);
intent.putExtra(EXTRA_PARAM3, param2);
context.startService(intent);
}
}
package com.example.hornsey.myapplication.IntentServiceDemo;
import android.app.IntentService;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import java.util.concurrent.TimeUnit;
/**
* An {@link IntentService} subclass for handling asynchronous task requests in
* a service on a separate handler thread.
*
* helper methods.
*/
public class MyIntentService extends IntentService {
private static final String TAG = "MyIntentService";
private static final String ACTION_FOO = "com.example.hornsey.myapplication.IntentServiceDemo.action.FOO";
private static final String ACTION_BAZ = "com.example.hornsey.myapplication.IntentServiceDemo.action.BAZ";
private static final String EXTRA_PARAM1 = "Name";
private static final String EXTRA_PARAM2 = "Question";
private static final String EXTRA_PARAM3 = "Answer";
public MyIntentService() {
super("MyIntentService");
}
@Override
protected void onHandleIntent(Intent intent) {
if (intent != null) {
final String action = intent.getAction();
if (ACTION_FOO.equals(action)) {
final String param1 = intent.getStringExtra(EXTRA_PARAM1);
final String param2 = intent.getStringExtra(EXTRA_PARAM2);
handleActionFoo(param1, param2);
} else if (ACTION_BAZ.equals(action)) {
final String param1 = intent.getStringExtra(EXTRA_PARAM1);
final String param2 = intent.getStringExtra(EXTRA_PARAM3);
handleActionBaz(param1, param2);
}
}
}
/**
* Handle action Foo in the provided background thread with the provided
* parameters.
*/
private void handleActionFoo(String param1, String param2) {
Log.d(TAG, "handleActionFoo." + param1 + " : " + param2);
try {
TimeUnit.SECONDS.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
/**
* Handle action Baz in the provided background thread with the provided
* parameters.
*/
private void handleActionBaz(String param1, String param2) {
Log.d(TAG, "handleActionBaz." + param1 + " : " + param2);
}
public MyIntentService(String name) {
super(name);
Log.d(TAG, "MyIntentService: ");
}
@Override
public IBinder onBind(Intent intent) {
Log.d(TAG, "onBind: ");
return super.onBind(intent);
}
@Override
public void onCreate() {
super.onCreate();
Log.d(TAG, "onCreate: ");
}
@Override
public void onDestroy() {
Log.d(TAG, "onDestroy: ");
super.onDestroy();
}
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
Log.d(TAG, "onStart: ");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG, "onStartCommand: ");
return super.onStartCommand(intent, flags, startId);
}
}
02-26 11:10:07.635 6624-6624/com.example.hornsey.myapplication D/MyIntentService: onCreate:
02-26 11:10:07.635 6624-6624/com.example.hornsey.myapplication D/MyIntentService: onStartCommand:
02-26 11:10:07.635 6624-6624/com.example.hornsey.myapplication D/MyIntentService: onStart:
02-26 11:10:07.645 6624-7217/com.example.hornsey.myapplication D/MyIntentService: handleActionFoo.Jack : How are you?
02-26 11:10:09.185 6624-6624/com.example.hornsey.myapplication D/MyIntentService: onStartCommand:
02-26 11:10:09.185 6624-6624/com.example.hornsey.myapplication D/MyIntentService: onStart:
02-26 11:10:12.645 6624-7217/com.example.hornsey.myapplication D/MyIntentService: handleActionBaz.Tom : Fine!
02-26 11:10:12.645 6624-6624/com.example.hornsey.myapplication D/MyIntentService: onDestroy:
02-26 11:10:33.855 6624-6624/com.example.hornsey.myapplication D/MyIntentService: onCreate:
02-26 11:10:33.855 6624-6624/com.example.hornsey.myapplication D/MyIntentService: onStartCommand:
02-26 11:10:33.855 6624-6624/com.example.hornsey.myapplication D/MyIntentService: onStart:
02-26 11:10:33.855 6624-7668/com.example.hornsey.myapplication D/MyIntentService: handleActionFoo.Jack : How are you?
02-26 11:10:38.865 6624-6624/com.example.hornsey.myapplication D/MyIntentService: onDestroy:
02-26 11:10:41.915 6624-6624/com.example.hornsey.myapplication D/MyIntentService: onCreate:
02-26 11:10:41.915 6624-6624/com.example.hornsey.myapplication D/MyIntentService: onStartCommand:
02-26 11:10:41.915 6624-6624/com.example.hornsey.myapplication D/MyIntentService: onStart:
02-26 11:10:41.925 6624-7790/com.example.hornsey.myapplication D/MyIntentService: handleActionBaz.Tom : Fine!
02-26 11:10:41.925 6624-6624/com.example.hornsey.myapplication D/MyIntentService: onDestroy: