我是这么写的:启动一个服务,用服务启动service来启动activity。可是一旦退出,就会循环启动。 请问,我该怎么解决?还是换个方法?就是一个应用的界面,上面有个开机启动的复选框,选中关机重启后自动运行程序。否则就不运行

解决方案 »

  1.   

    在service中多加个判断标志,以确保一个应用程序只会被启动一次
      

  2.   

    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);
            
            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;
    }
    }






    }
    现在它循环~
      

  3.   

    用不了那么麻烦吧
    监听到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);
     }
    }
    }
      

  4.   

    大侠,我也想使用checkbox对开机时自启动程序进行控制。目前我遇到一些问题,我使用list把自启动的程序显示出来后,对部分listitem采用checkbox进行选择后,当再次打开程序时,并没有显示上次选中的选项。我采用的存储方式是sharedpreferences方式,在key/vaule(packagename,true/false)中已经能够显示packagename,但是后面的这一项当选中时我希望是时true,但是选中后依然是初始化的状态false。请大侠给点指导和建议。谢谢。
      

  5.   

     你的这个问题跟我的不太一样吧,你先处理好sharedpreferences,这个只能存一个的,你要存多个就不行了。你的是管理启动项,跟我的还是有点区别