Android实现开机自启动Service
2010-06-22 02:26:00 来源:WEB开发网private int flg;
private Intent tsIntent;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button01 = (Button)findViewById(R.id.Button01);
button02 = (Button)findViewById(R.id.Button02);
tsIntent = this.getIntent();
Bundle bundle = tsIntent.getExtras();
if (bundle == null){
flg = 1;
DisplayToast(this,"Service Start",2);
startService();
finish();
}else{
DisplayToast(this,"Service Stop",2);
stopService();
finish();
}
button01.setOnClickListener(Listener01);
button02.setOnClickListener(Listener02);
}
Button.OnClickListener Listener01 = new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
startService();
}
};
Button.OnClickListener Listener02 = new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
stopService();
}
};
private void startService() {
Intent i = new Intent(testService.this, TService.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
this.startService(i);
}
private void stopService() {
Intent i = new Intent(testService.this, TService.class);
this.stopService(i);
}
public static void DisplayToast(Context context , String str , int time){
if (time == 1){
//短时间显示Toast
Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
}else if (time == 2){
更多精彩
赞助商链接