LoggingServiceClient.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class LoggingServiceClient extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); final Button messageButton = (Button) findViewById(R.id.message_button); messageButton.setOnClickListener(new OnClickListener() { public void onClick(View v) { // Create an Intent for starting the LoggingService Intent startServiceIntent = new Intent(getApplicationContext(), LoggingService.class); // Put Logging message in intent startServiceIntent.putExtra(LoggingService.EXTRA_LOG, "Log this message"); // Start the Service startService(startServiceIntent); } }); } } |
LoggingService.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import android.app.IntentService; import android.content.Intent; import android.util.Log; public class LoggingService extends IntentService { public static String EXTRA_LOG = "course.examples.Services.Logging.MESSAGE"; private static final String TAG = "LoggingService"; public LoggingService() { super(TAG); } @Override protected void onHandleIntent(Intent intent) { Log.i(TAG, intent.getStringExtra(EXTRA_LOG)); } } |
main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <Button android:id="@+id/message_button" android:layout_width="match_parent" android:layout_height="64dp" android:layout_alignParentBottom="true" android:text="@string/generate_log_message_string" android:textSize="24sp" > </Button> </RelativeLayout> |