public class LoadBootReceiver extends BroadcastReceiver {
static final String ACTION = "android.intent.action.BOOT_COMPLETED";
@Override public void onReceive(Context arg0, Intent arg1) { // TODO Auto-generated method stub if(arg1.getAction().equals(ACTION)){ Intent service = new Intent(arg0,CeshiService.class); service.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); arg0.startService(service);
} }
}这个是开机启动广播然后启动服务CeshiService. public class LediService extends Service{ // Binder given to clients private final IBinder mBinder = new LocalBinder(); // private final Random mGenerator = new Random(); public class LocalBinder extends Binder { LediService getService() {
return LediService.this; } } @Override public IBinder onBind(Intent intent) { Intent bootActivityIntent=new Intent(getApplicationContext(),ShowAcitvity .class); bootActivityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(bootActivityIntent); return mBinder; }} 这个是我写的服务,用它启动showactivity下面是有复选框的: public class LoginActivity extends Activity { /** Called when the activity is first created. */ private CheckBox btn; SharedPreferences sp; Editor editor; boolean mBound=false; CeshiService mService; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); btn=(CheckBox)findViewById(R.id.btn);
static final String ACTION =
"android.intent.action.BOOT_COMPLETED";
@Override
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
if(arg1.getAction().equals(ACTION)){
Intent service = new Intent(arg0,CeshiService.class);
service.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
arg0.startService(service);
}
}
}这个是开机启动广播然后启动服务CeshiService.
public class LediService extends Service{ // Binder given to clients
private final IBinder mBinder = new LocalBinder(); // private final Random mGenerator = new Random();
public class LocalBinder extends Binder {
LediService getService() {
return LediService.this;
}
} @Override
public IBinder onBind(Intent intent) {
Intent bootActivityIntent=new Intent(getApplicationContext(),ShowAcitvity .class);
bootActivityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(bootActivityIntent);
return mBinder;
}} 这个是我写的服务,用它启动showactivity下面是有复选框的:
public class LoginActivity extends Activity { /** Called when the activity is first created. */
private CheckBox btn;
SharedPreferences sp;
Editor editor;
boolean mBound=false;
CeshiService mService;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn=(CheckBox)findViewById(R.id.btn);
sp=getSharedPreferences("open",MODE_PRIVATE);
if(sp.getInt("open", 0)==1){
btn.setChecked(true);
}
btn.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
// TODO Auto-generated method stub
if(btn.isChecked()){
editor=sp.edit();
editor.putInt("open", 1);
editor.commit();
}else {
editor=sp.edit();
editor.putInt("open", 2);
editor.commit();
}
}
});
}
private ServiceConnection mConnection=new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName arg0) {
// TODO Auto-generated method stub
mBound=false;
}
@Override
public void onServiceConnected(ComponentName arg0, IBinder arg1) {
// TODO Auto-generated method stub
LocalBinder binder = (LocalBinder) arg1;
mService = binder.getService();
mBound = true; }
};
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
if(sp.getInt("open", 0)==1){
btn.setChecked(true);
Intent intent = new Intent(this, LediService.class);
bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
}
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
if(mBound){
unbindService(mConnection);
mBound=false;
}
}
}
现在它循环~
监听到ACTION_BOOT_COMPLETED后直接启动不就可以了吗public class LoadBootReceiver extends BroadcastReceiver {static final String ACTION =
"android.intent.action.BOOT_COMPLETED";@Override
public void onReceive(Context arg0, Intent arg1) {
if(arg1.getAction().equals(ACTION)){
// 读取自动启动flag,然后 if(flag == "1"){
Intent bootActivityIntent=new Intent(arg0,ShowAcitvity .class);
bootActivityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(bootActivityIntent);
}
}
}