Android - 如何在开机后启动位于 /sdcard 上的应用程序

20

如何在Android系统启动时自动启动位于/sdcard的应用程序?

可以通过BroadcastReceiver实现。但是,应该使用哪个操作呢?

ACTION_BOOT_COMPLETED - does not work if it is on the /sdcard (documented)
ACTION_MEDIA_MOUNTED - does not work if it is on the /sdcard (which is undocumented)
ACTION_EXTERNAL_APPLICATIONS_AVAILABLE - does not work, I do not know why
ACTION_USER_PRESENT - does not work if the BroadcastReceiver is registered in AndroidManifest (which is undocumented, but documentation bug has been reported)

谢谢
Jan


你解决过这个问题吗?我现在有一个类似的问题。 - M Rajoy
你的问题帮助我找到了答案,谢谢。 :D - Kamran Ahmed
如果你觉得这个答案有帮助,你应该接受它。 - Kamran Ahmed
我很抱歉,对于这个问题唯一正确的答案是:不,没有办法。 - BladeCoder
这个问题的正确答案在这里:http://stackoverflow.com/questions/8248617/run-a-service-when-device-starts-after-sd-finishes-loading,它是创建一个接收器来同时处理 BOOT_COMPLETEDMEDIA_MOUNTED 事件。 - Bron Davies
4个回答

1

1
请尝试使用以下代码:<receiver android:name=".BootCompleteReceiver" > <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> <action android:name="android.intent.action.QUICKBOOT_POWERON" /> </intent-filter> </receiver>以及<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />。也许QUICKBOOT_POWERON可以帮助您。

1
请在清单文件中提及它。
</uses-permission>    
<receiver android:name=".BootReceiver"
    android:enabled="true"
    android:exported="true"
    android:label="BootReceiver">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED"></action>
    </intent-filter>
</receiver>

将"android.permission.RECEIVE_BOOT_COMPLETED"权限作为manifest的子元素提供。

还有一件事,您的应用程序不能安装在SD卡中。


这并没有回答问题,因为问题明确要求在应用程序安装在SD卡上时如何在启动时收到通知。 - BladeCoder
我不确定,但在我看来是不可能的。 - Rahul Chaudhary
我同意,所以正确的答案是不可能的。 - BladeCoder

0

我通常会为广播接收器注册每个意图过滤器,包括在 AndroidManifest 中以及在扩展 Application 类的类中动态注册。

在 AndroidManifest.xml 中:

    <receiver
            android:name=".broadcastReciever"
            android:enabled="true"
            android:exported="true" android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <action android:name="android.intent.action.EXTERNAL_APPLICATIONS_AVAILABLE" />
            </intent-filter>
        </receiver>

并且在一个继承Application的类中:

registerReceiver(new broadcastReciever(), new IntentFilter(Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE));

不要忘记添加RECEIVE_BOOT_COMPLETED权限,并在Android清单中注册扩展Application的类。

这应该可以了;如需更多帮助/澄清,请随时提问。


为什么两种方式都要呢? - tmm1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接