【问题标题】:Android app needs to be auto-restart after the phone power OnAndroid应用需要在手机开机后自动重启
【发布时间】:2012-12-20 11:50:43
【问题描述】:

我需要在手机重启和开机后自动启动我的应用程序一次。

我使用了AutoStart an Application at boot up 提供的代码,现在我的安卓应用在手机重启(重启)后自动启动。

现在,考虑一下,我没有使用重启手机,而是使用了关闭手机电源(关闭手机) 选项。手机开机后,我的应用程序没有按预期自动启动。你能解释一下我错过了什么吗?

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

<receiver android:enabled="true" android:name=".BootUpReceiver" android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </receiver>



public class BootUpReceiver extends BroadcastReceiver
{
    private static SharedPreferences aSharedSettings;

    @Override
    public void onReceive(Context context, Intent intent) 
    {
        aSharedSettings = context.getSharedPreferences("MYPreferences", Context.MODE_PRIVATE);
        boolean isUserLoggedIn = aSharedSettings.getBoolean(kEY.AUTHENTICATED, false); 
        if(isUserLoggedIn) 
        {
Intent aServiceIntent = new Intent(context, HomeView.class);
                aServiceIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(aServiceIntent); 
}

    }
}

谢谢。

【问题讨论】:

    标签: android


    【解决方案1】:

    如果您在重新启动手机之前至少手动启动一次应用程序,而不是“强制关闭”它,它是否按预期工作?

    看看:

    Android : android.intent.action.BOOT_COMPLETED on ICS and Gingerbread

    Boot Completed Regression Confirmed

    【讨论】:

      【解决方案2】:

      您可以尝试一些事情。

      首先检查AndroidManifest.xml 中的应用installLocation 是否设置为android:installLocation="internalOnly",这样可以确保应用在本地存储上。安装到 sdcard 的应用将不会收到 BOOT_COMPLETE 意图。

      另外我会删除&lt;category android:name="android.intent.category.DEFAULT" /&gt;,这是没有必要的。

      您可以尝试的最后一件事是使用完整的包名:

          <receiver android:enabled="true" 
                    android:name="com.myapp.receivers.BootUpReceiver" 
                    android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >
              <intent-filter>
                  <action android:name="android.intent.action.BOOT_COMPLETED" />
              </intent-filter>
          </receiver>
      

      【讨论】:

      • 您好,感谢您的回复。我进行了所有 3 项更改。但它不起作用。我正在使用 HTC one V。对我来说,在 HTC ONE V 中,我的 android 应用程序会在手机重启(重启)后自动启动。但是,当我使用“Phone power off (Shut phone down)”时,没有调用此 BroadcastReceiver,并且重新启动不起作用。所以我觉得,BroadcastReceiver没有问题。我用其他android模型检查了这个,发现自动重启在“手机关机”中正常工作。 HTC one V 中唯一的问题。请有任何建议..
      • 所以简单地说,它在重新启动时有效,但在您来自“关机”> 时无效?
      • 是的,Chris .. 它在重新启动时有效,但在“关机”时无效。
      • 我只能看到,作为一个设备错误,没有“特殊”方式来指定您是否来自清单中的重新启动/启动。 ://
      【解决方案3】:

      尝试添加

      <category android:name="android.intent.category.LAUNCHER" />
      

      而不是

      <category android:name="android.intent.category.DEFAULT" />. 
      

      还要检查 isUserLoggedIn 的值。

      【讨论】:

      • 谢谢库马尔。试过这个。但它仍然无法在 HTC one V 中的手机关机(关闭手机)中工作。但是,在其他型号的手机中它可以工作。请提出建议..
      【解决方案4】:

      您使用的是 HTC 设备吗?如果是这样,您可能启用了一个名为“快速启动”的功能

      查看此链接了解详情。

      Detect if HTC "Fast boot" is enabled

      【讨论】:

      • 您好,感谢您的快速响应。是的,我正在使用 HTC one V。对我来说,在 HTC ONE V 中,我的 android 应用程序会在手机重启(重启)后自动启动。但是,当我使用“Phone power off (Shut phone down)”时,没有调用此 BroadcastReceiver,并且重新启动不起作用。我用其他安卓机型(三星和索尼)检查了这一点,发现自动重启在“手机关机”中正常工作。 HTC one V 中唯一的问题。我尝试了 ACTION_SCREEN_ON 和 ACTION_USER_PRESENT,但它不起作用。请帮忙..
      【解决方案5】:

      您启动应用程序的活动名称,在您的标签中添加这一行.....并让我知道它是否有效

        <category android:name="android.intent.category.HomeView" />
      

      【讨论】:

      • 感谢 samintechvalens 的回复。我试过了,但是还是不行。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-10
      • 1970-01-01
      相关资源
      最近更新 更多