【问题标题】:How to start an application in android when mobile is switched ON?手机开机时如何在android中启动应用程序?
【发布时间】:2012-02-09 12:20:54
【问题描述】:

嘿,我有一个使用后台服务的应用程序。它运行正常。如果我的手机关机,我的服务是 ound 服务关闭。当我的应用程序启动时,只有我的后台服务是分层的。我想再次重新启动服务手机什么时候关机? 可能吗? 谁用代码解释一下

更新

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


                        }
        }   
                }

}

【问题讨论】:

    标签: android restart background-service


    【解决方案1】:

    注册ACTION_BOOT_COMPLETED (see here for details)。在启动时启动您的服务。

    【讨论】:

    • receiver = new ConnectionReceiver(); registerReceiver(receiver,new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));像这样启动我的服务和我的 我的清单文件。但是我的服务没有启动。
    【解决方案2】:

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

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

    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中的broadcastreciever作为

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

    【讨论】:

    • receiver = new ConnectionReceiver(); registerReceiver(receiver,new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));像这样启动我的服务和我的 我的清单文件。但是我的服务没有启动。
    • 你在哪里盯着服务,你把 startservice 的意图放在哪里?请把代码..
    【解决方案3】:

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

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

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

    【讨论】:

      猜你喜欢
      • 2013-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-14
      • 2011-04-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多