MyService.java
package com.AndroidServiceTest;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.widget.Toast;
public class MyService extends Service {
MyServiceReceiver myServiceReceiver;
final static String MY_ACTION = "MY_ACTION";
final static String MY_ACTION_FROMACTIVITY = "MY_ACTION_FROMACTIVITY";
public static final String CMD = "CMD";
public static final int CMD_STOP = 1;
boolean running;
String initData;
@Override
public void onCreate() {
// TODO Auto-generated method stub
myServiceReceiver = new MyServiceReceiver();
super.onCreate();
}
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(MY_ACTION_FROMACTIVITY);
registerReceiver(myServiceReceiver, intentFilter);
running = true;
initData = intent.getStringExtra("INIT_DATA");
MyThread myThread = new MyThread();
myThread.start();
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
this.unregisterReceiver(myServiceReceiver);
super.onDestroy();
}
public class MyThread extends Thread{
@Override
public void run() {
// TODO Auto-generated method stub
int i = 0;
while(running){
try {
Thread.sleep(5000);
Intent intent = new Intent();
intent.setAction(MY_ACTION);
intent.putExtra("DATAPASSED", i);
intent.putExtra("DATA_BACK", initData);
sendBroadcast(intent);
i++;
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
stopSelf();
}
}
public class MyServiceReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
int hostCmd = arg1.getIntExtra(CMD, 0);
if(hostCmd == CMD_STOP){
running = false;
stopSelf();
Toast.makeText(MyService.this,
"Service stopped by main Activity!",
Toast.LENGTH_LONG).show();
}
}
}
}
Modify main activity (AndroidServiceTestActivity.java) to send command to stop our service, by pressing of the Stop button.
package com.AndroidServiceTest;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class AndroidServiceTestActivity extends Activity {
MyReceiver myReceiver;
Button btnStop;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnStop = (Button)findViewById(R.id.stop);
btnStop.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setAction(MyService.MY_ACTION_FROMACTIVITY);
intent.putExtra(MyService.CMD, MyService.CMD_STOP);
sendBroadcast(intent);
}});
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
//Register BroadcastReceiver
//to receive event from our service
myReceiver = new MyReceiver();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(MyService.MY_ACTION);
registerReceiver(myReceiver, intentFilter);
//Start our own service
Intent intent = new Intent(AndroidServiceTestActivity.this,
com.AndroidServiceTest.MyService.class);
intent.putExtra("INIT_DATA", "Data passed from Activity to Service in startService");
startService(intent);
super.onStart();
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
unregisterReceiver(myReceiver);
super.onStop();
}
private class MyReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
int datapassed = arg1.getIntExtra("DATAPASSED", 0);
String orgData = arg1.getStringExtra("DATA_BACK");
Toast.makeText(AndroidServiceTestActivity.this,
"Triggered by Service!\n"
+ "Data passed: " + String.valueOf(datapassed) + "\n"
+ "original Data: " + orgData,
Toast.LENGTH_LONG).show();
}
}
}
Modify layout to add the Stop button.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:id="@+id/stop"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Stop"
/>
</LinearLayout>
Hi,
ReplyDeleteLast many days I am searching some good article and I am very glad to find your article. This is very essential and informative information for me. I would like to say your post is superb and relevant my topics.
Thanks
Hi, im a little worried about you since long time without a post, hope you are ok.
ReplyDeleteHi zuka,
ReplyDeleteI'm OK.
No post for a long-time because i just have a short trip to Taipei.
Thx:)
Thanks alot
ReplyDeleteThat was very useful