简体   繁体   English

启动应用程序启动完成

[英]Starting application on boot completed

The following is the code I'm using for starting my Application when device is turned on. 以下是我打开设备时用于启动应用程序的代码。

public class BootReceiver extends BroadcastReceiver{
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i("BootReceiver","intent received");

        Intent myIntent = new Intent(context, ACT_Home.class);
        myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(myIntent);
    }

}

and in the Manifest (as <Application> child): 并在清单中(作为<Application> child):

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

The permissions inside the Manifest are the following: Manifest中的权限如下:

<uses-permission android:name="android.permission.INSTALL_PACKAGES" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.VIBRATE" />
<uses-feature android:name="android.hardware.camera"/>
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.DISABLE_KEYGUARD"/>

This works fine on Android 3.2.2, but if I try the same application on Android 4.0.3 the broadcast receiver does not receive anything. 这在Android 3.2.2上工作正常,但如果我在Android 4.0.3上尝试相同的应用程序,广播接收器不会收到任何东西。 Also the first line inside the onReceive method is not execeuted. 此外, onReceive方法中的第一行未被强制执行。 Why this happens? 为什么会这样?

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

这应该在android清单中使用

Try this, although your code seems fine! 试试这个,虽然你的代码看起来很好! The following is working for me. 以下是为我工作。

    <!-- Receivers -->
    <receiver android:enabled="true" android:name="host.alarmmanager.BootReceiver"
        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>

Make sure you are not restarting your phone by selecting restart option from the power menu. 通过从电源菜单中选择重启选项,确保没有重新启动手机。

Android bizarrely has 2 different permissions. Android奇怪有2个不同的权限。

1.Reboot 1.Reboot

2.On boot complete 2.开机完成

So, first power off your phone and then after few seconds power on it again! 所以,首先关闭手机电源然后再打开电源几秒钟!

Hope it helps! 希望能帮助到你! (Y) (Y)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM