如何在移动设备开启时在Android中启动应用程序?

时间:2012-01-13 09:11:04

标签: android restart background-service

我有一个使用后台服务的应用程序。运行清楚。如果我的手机关机,我的服务是关闭服务。当我的应用程序启动时,只有我的后台服务被控制。我想再次重启服务手机关机时? 可能吗? 任何人都用代码解释

更新

public class loginForm extends Activity 
{
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView (R.layout.login);
receiver = new ConnectionReceiver();
 registerReceiver(receiver,new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
}
 }

private class ConnectionReceiver extends BroadcastReceiver{

        private Timer mTimer;
        private TimerTask mTimerTask;

        @Override
        public void onReceive(Context context, Intent intent) {
                NetworkInfo info = intent.getParcelableExtra         (ConnectivityManager.EXTRA_NETWORK_INFO);

                if(null != info)
                {
                        String state = getNetworkStateString(info.getState());
                        if(state.equals("Connected")){
                            mTimer = new Timer();
                            mTimerTask = new TimerTask() {
                                @Override
                                public void run() {
                                    loginForm.this.runOnUiThread(new Runnable() {

                                        @Override
                                        public void run() {

                                            //Toast.makeText(getBaseContext(), "Disenabled provider " + provider,
                                                    ///Toast.LENGTH_SHORT).show();  
                                            try{
                                            insertAllGpsInformation();
                                            }
                                            catch(Exception e)
                                         {
                                            Toast.makeText(getBaseContext(), "Your Net Connected or Not Login to Net"+"", Toast.LENGTH_LONG).show();
                                            Log.e("Upload Picture Error:",e.getMessage());
                                         }
                                        }


                                    });

                               }
                            };
                            mTimer.scheduleAtFixedRate(mTimerTask,180000,180000);
                        }


                        }
        }   
                }

}

3 个答案:

答案 0 :(得分:0)

注册ACTION_BOOT_COMPLETEDsee here for details)。启动时启动服务。

答案 1 :(得分:0)

您应该注册一个BroadcastReceiver并查找BOOT_COMPLETED Intent。

以下是一些详细信息的链接:http://androidgps.blogspot.com/2008/09/starting-android-service-at-boot-time.html

我是否正确理解了您的问题?

答案 2 :(得分:0)

使用BOOT COMPLETED Action的意图创建广播接收器。

请参阅以下链接以获取更多帮助:

  1. http://blog.gregfiumara.com/?p=82
  2. http://marakana.com/forums/android/examples/60.html
  3. 您可以使用

    public class BootReceiver extends BroadcastReceiver
    {
       @Override
       public void onReceive(Context context, Intent intent)
       {
          // TODO Auto-generated method stub
          Log.i("BootReceiver :: Start Booting..");
    
          Intent i = new Intent(context, StartService.class);    // Start your service class
               i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
               context.startActivity(i);    
       }
       }
    

    并使用androidmanifest.xml中的broadcastreciever作为

    <receiver android:name=".receiver.BootReceiver">
    <intent-filter> 
    <action android:name="android.intent.action.BOOT_COMPLETED" /> 
    </intent-filter> 
    </receiver>