WEB开发网
开发学院手机开发Android 开发 Android实现开机自启动Service 阅读

Android实现开机自启动Service

 2010-06-22 02:26:00 来源:WEB开发网   
核心提示:private int flg;private Intent tsIntent;/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreat

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){

上一页  1 2 3 4  下一页

Tags:Android 实现 开机

编辑录入:coldstar [复制链接] [打 印]
赞助商链接