0

海我有一个使用后台服务的应用程序。它运行清晰。如果我的手机关闭,我的服务是关闭服务。当我的应用程序启动时,只有我的后台服务被分层。我想在手机切换时再次重新启动服务离开?是否可以?任何人用代码解释

更新

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


                        }
        }   
                }

}
4

3 回答 3

0

注册ACTION_BOOT_COMPLETED详见此处)。在启动时启动您的服务。

于 2012-01-13T09:13:50.627 回答
0

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

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

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

于 2012-01-13T09:15:25.173 回答
0

创建具有 BOOT COMPLETED 操作意图的广播接收器。

请参考以下链接以获得更多帮助:

  1. http://blog.gregfiumara.com/?p=82
  2. http://marakana.com/forums/android/examples/60.html

您可以使用

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 中的广播接收器作为

<receiver android:name=".receiver.BootReceiver">
<intent-filter> 
<action android:name="android.intent.action.BOOT_COMPLETED" /> 
</intent-filter> 
</receiver>
于 2012-01-13T09:18:01.977 回答