2011年7月12日 星期二

Android - Service and Broadcast

原本想在Android弄弄看怎樣在一開機時跑某apk(準確來說是開機完成後),

找到了broadcast這種廣播方式,可以設定要接收的Action(像是event)

也就是當開機完成後系統會有一個事件(叫做BOOT_COMPLETED)被廣播

只要接收到這事件後用intent開activity就OK了

後來發現把service和broadcast摻再一起做撒尿牛丸好像還蠻有趣的

就順便研究囉!

方式如下:



先開新的project後(BoottestActivity)在package中new一個class

bootreceive.java:
 ===========================================

package diousk.com;


import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;

public class bootreceive extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
  Log.d("Receiver","Get event : "+intent.getAction());//看接收到的是甚麼事件


        if(intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)){//如果是  ACTION_BOOT_COMPLETED 事件

Intent bootActivityIntent=new Intent(context,BoottestActivity.class);
              bootActivityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);//未知flag@@
              context.startActivity(bootActivityIntent);//開啟BoottestActivity
}
else{
int dataint=intent.getIntExtra("i", -1);
Log.d("Receiver","Get data : "+dataint);
}
}
public bootreceive()
{
Log.d("Receiver","receiver construtor");
}


}

===========================================



BoottestActivity.java:
===========================================

 package diousk.com;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class BoottestActivity extends Activity {
Button mybtn1,mybtn2;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
        setContentView(R.layout.main);//下面都和service有關,不要service的話到這即可
mybtn1=(Button)findViewById(R.id.mystart);
mybtn2=(Button)findViewById(R.id.mystop);

mybtn1.setOnClickListener(new Button.OnClickListener() //按下這個鈕會run BootqqService 
{
public void onClick(View arg0)
{
          startService(new Intent(BoottestActivity.this,BootqqService.class));
}
});

mybtn2.setOnClickListener(new Button.OnClickListener()  //按下這個鈕會stop BootqqService 
{
public void onClick(View arg0)
{
stopService(new Intent(BoottestActivity.this,BootqqService.class));
}
});

}
}

===========================================

BootqqService.java  
 ===========================================  

package diousk.com;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.util.Log;

public class BootqqService extends Service{
boolean isStop=false;
BroadcastReceiver br;
@Override
 public IBinder onBind(Intent arg0) {//一定要實現的method,還不知道作何用途
// TODO Auto-generated method stub
Log.d("qqService","Binded!!");
return null;
}

public void onCreate(){
Log.i("qqService","Services onCreate");
super.onCreate();

  br=new bootreceive();//註冊一個自己定義的事件叫做diouskevent.mmn, 如果有人廣播的話就可以被接收到了 
IntentFilter intfilter=new IntentFilter();
intfilter.addAction("diouskevent.mmn");
  registerReceiver(br,intfilter);

}


public void onStart(Intent intent,int startId){
Log.i("qqService","Services onStart");
super.onStart(intent, startId);
  new Thread(){//跑一個thread每一秒送一個diouskevent.mmn的廣播
public void run(){
int i=0;

while(!isStop){
Log.i("qqService","Services send integer");
Intent intent=new Intent();
intent.putExtra("i", i);
i++;
intent.setAction("diouskevent.mmn");
sendBroadcast(intent);
try {
sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}.start();

}


@Override
public void onDestroy() {
Log.i("qqService","Services onDestory");
isStop=true;
super.onDestroy();
}
}

===========================================  

AndroidManifest.xml(要註冊service和receiver以及接收BOOT_COMPLETED的權限)
===========================================


   
    
    
   
  
  
  




===========================================






That's it.

沒有留言: