編輯:關於android開發
Android 開機自啟動示例程序。使用廣播方式接受,采用Android自帶存儲SharedPreferences存儲開機自啟動的設置。
本文源碼:點擊
package com.example.autostart;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.Intent;
import android.content.SharedPreferences;
//開機自啟動廣播接受
public class AutoStartBroadcastReceiver extends BroadcastReceiver {
private static final String ACTION = android.intent.action.BOOT_COMPLETED;
private SharedPreferences mPreferences = null;
@Override
public void onReceive(Context context, Intent intent) {
mPreferences = context.getSharedPreferences(AutoStart,
ContextWrapper.MODE_PRIVATE);
if (intent.getAction().equals(ACTION)) {
if (mPreferences.getBoolean(AddToAuto, false)) {
//後邊的XXX.class就是要啟動的服務
Intent service = new Intent(context,AutoStartService.class);
context.startService(service);
// 啟動應用,參數為需要自動啟動的應用的包名,只是啟動app的activity的包名
Intent newIntent = context.getPackageManager()
.getLaunchIntentForPackage(com.example.autostart);
context.startActivity(newIntent);
}
}
}
}
如果程序需要啟動一些必要的服務再寫這個也可以,一般開機自啟動只需要啟動app的主activity。這裡示范一下寫服務。
package com.example.autostart;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.IBinder;
import android.util.Log;
//開機自啟動廣播接受
public class AutoStartService extends Service {
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate(){
super.onCreate();
Log.d(TAG2,test service);
}
}
package com.example.autostart;
import android.os.Bundle;
import android.app.Activity;
import android.content.ContextWrapper;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
public class MainActivity extends Activity {
private SharedPreferences mPreferences = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mPreferences = getSharedPreferences(AutoStart,ContextWrapper.MODE_PRIVATE);
boolean bStart = mPreferences.getBoolean(AddToAuto, false);
final TextView textView1 = (TextView)findViewById(R.id.textView1);
if (bStart) {
textView1.setText(已打開開機自啟動);
}else {
textView1.setText(已關閉開機自啟動);
}
//打開
findViewById(R.id.button1).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Editor editor = mPreferences.edit();
editor.putBoolean(AddToAuto, true);
editor.commit();
textView1.setText(已打開開機自啟動);
}
});
//關閉
findViewById(R.id.button2).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Editor editor = mPreferences.edit();
editor.putBoolean(AddToAuto, false);
editor.commit();
textView1.setText(已關閉開機自啟動);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}

android入門系列- TextView EditText Button ImageView 的簡單應用,textviewedittext
android入門系列- TextView EditText Button ImageView 的簡單應用,textviewedittext 第一篇原創,其實
Android消息機制之Handler,androidhandler
Android消息機制之Handler,androidhandlerAndroid為什麼要提供Handler Android建議我們不要在UI線程中執行耗時操作,因為這很
Android中開發工具Android Studio修改created用戶(windows環境),androidcreated
Android中開發工具Android Studio修改created用戶(windows環境),androidcreated最近經常有朋友反饋說我的安卓項目中,在一些類
學習Android從0開始之背景篇-Android系統介紹
學習Android從0開始之背景篇-Android系統介紹 android系統介紹 Android(安卓),是一個以Linux為基礎的開源移動設備操作系統,主要用於智能手