【问题标题】:launch android app on device startup在设备启动时启动 android 应用程序
【发布时间】:2015-10-28 21:02:36
【问题描述】:

当我打开我的 Android 设备时,我正在尝试启动我的应用程序。目前我有一个 BroadcastReciever 类和一个 Service 类,但是当我重新启动我的设备时,该应用程序似乎没有启动。

我的广播接收器类

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

        if (intent.getAction().equalsIgnoreCase(Intent.ACTION_BOOT_COMPLETED)) {
        Toast.makeText(context, "Reboot completed! Starting your app!!!.", Toast.LENGTH_LONG).show();

            Intent i = new Intent(context, AutoStart.class);
            i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startService(i);
        }

    }
}

我的服务类

public class AutoStart extends Service {

    @Override
    public void onCreate() {
        super.onCreate();
        Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
    }

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

我的清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.test"
android:sharedUserId="android.uid.system">

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="19" />

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

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

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

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

    <activity
        android:name=".MyActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".Result"
        android:label="@string/title_activity_result"
        android:parentActivityName=".MyActivity" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.example.test.MyActivity" />
    </activity>

    <service
        android:name=".functionality"
        android:enabled="true" />
</application>

</manifest>

有人能指出我正确的方向吗?我不确定我做错了什么。

【问题讨论】:

标签: android service broadcastreceiver


【解决方案1】:

在启动接收器开始工作之前,您的应用程序需要至少手动运行一次,而且启动意图可能会在相当长的一段时间后发送,甚至可能被其他应用程序拦截和杀死。我在少数第三方短信应用中遇到过这种行为。

【讨论】:

    【解决方案2】:

    我对此并不完全确定,但请尝试从 BootBroadcastReceiver 中删除 android:exported="false"android:exported="false" 表示没有其他应用可以调用它,系统在这里就像一个应用。

    【讨论】:

    • hmm 所以我摆脱了 Service 类,在我的接收器类中我将 startService 更改为 startActivity(context, MainActivity.class);这有效。但是在启动此应用程序之前有 30 秒的延迟。知道为什么吗?
    • 这可能是 Android 中的安全策略,因此“不良”应用无法立即阻止设备。但是,无论如何您都不应该在启动时开始活动。
    【解决方案3】:

    尝试使用context.startActivity(i); 而不是context.startService(i);

    【讨论】:

      【解决方案4】:

      您应该在您的广播接收器类中更正此行: 使用:context.startActivity(i); 而不是:context.startService(i);

      【讨论】:

        猜你喜欢
        • 2020-02-10
        • 1970-01-01
        • 2014-04-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多