简体   繁体   English

如何在重启后在android中自动启动我的应用程序?

[英]how can I autostart my app in android after reboot?

I have used BootComplete and allow permission and it still cant autostart,then I try to use wake lock but it cannot work. 我已经使用了BootComplete并允许权限,它仍然无法自动启动,然后我尝试使用唤醒锁但它无法正常工作。 Also, I try to make it as a service but the service does not pop up in my phone.Is there anything I missed? 另外,我尝试将其作为一项服务,但该服务未在手机中弹出,我错过了什么吗?

       public class BootComplete extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {

{
            // This is the Intent to deliver to our service.
            Intent serviceIntent = new Intent(context, AutoStartUp.class);
            context.startService(serviceIntent);

        }
    }

public class AutoStartUp extends Service {

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {

        super.onCreate();
        Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
        // do something when the service is created
    }

}

In my manifest file: 在我的清单文件中:

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

 <service android:name=".SimpleWakefulReceiver">
            <intent-filter>
                <action android:name="com.example.SimpleWakefulReceiver"/>
            </intent-filter>
        </service>

        <receiver
            android:name=".MainActivity$BootComplete"
            android:enabled="true"
            android:exported="false">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>

        <service android:name=".AutoStartUp">
        </service>

Do whatever you intend in onReceive - 做任何你想要的事情 - 接受 -

public class BootupReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {

    try {
        Thread.sleep(5000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    Log.e("BOOTUP", "received notification ......................");
    if(intent.getAction().equalsIgnoreCase(Intent.ACTION_BOOT_COMPLETED))
    {
        Log.e("BOOTUP","RECEIVED BOOT NOTIFICATION ........");
        Intent start_service = new Intent(context,MainService.class);
        context.startService(start_service);
    }
}

in the Manifest add- 在清单清单中-

<receiver
        android:name=".AutoStart"
        android:enabled="true"
        android:exported="true">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>

add permission- 添加权限 -

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

Also you need to launch your application from activity atleast once and your block should be outside block in manifest 此外,您需要至少从活动启动一次应用程序,并且您的块应该在清单中的块之外

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

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