gpt4 book ai didi

Android 应用程序在启动时未运行

转载 作者:行者123 更新时间:2023-11-30 04:05:11 24 4
gpt4 key购买 nike

我有一个基本的 Android 应用程序,它应该连续重启手机一定次数。为此,我需要在手机启动时启动应用程序。为此,我基本上按照找到的说明进行操作 here ,将权限添加到 list 并创建启动 Activity 的 BroadcastReceiver 类。这是我的一些相关代码:

public class StartMyServiceAtBootReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
System.out.println("StartMyServiceAtBootReceiver called.");

if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
// Intent activityIntent = new Intent("com.example.rebooter.MainActivity");
Intent activityIntent = new Intent(context,MainActivity.class);
activityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(activityIntent);
}
}

来自 list 文件:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.rebooter"
android:versionCode="1"
android:versionName="1.0" >

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

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

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

<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</activity>

<service
android:name=".RebootManager"
android:label="Reboot Manager" >
<action android:name="com.example.rebooter.RebootManager" />
</service>

<receiver android:name=".StartMyServiceAtBootReceiver"
android:enabled="true"
android:exported="true"
android:label="StartMyServiceAtBootReceiver" >
<action android:name="android.intent.action.BOOT_COMPLETED" />
</receiver>

</application>

在 Eclipse 模拟器中,应用程序似乎运行正常。也就是说,虽然我的模拟器没有 root 并且手机没有正确执行重启命令,但我确实看到,在启动时,正确的 Activity 开始了。

现在,当我在运行 Android 4.0.4 的特定系统上尝试它时,应用程序中的所有内容都正常工作,除了启动时的引导。有任何想法吗?

我试图通过安装另一个在启动时启动并确认它确实在启动时启动并且确实显示出来的应用程序来消除任何硬件问题的可能性(因为我没有使用商业发布的手机)在启动后作为缓存进程运行应用程序。

如果有任何帮助,我将不胜感激。如果您需要任何其他信息,请告诉我。

最佳答案

这里有很多问题。

首先,您忽略了发布 StartMyServiceAtBootReceiver ,您希望在启动时获得控制权的组件,因此我们无法评论它是否存在任何特定问题。

其次,除非某些东西明确执行了您的组件之一(例如,用户从主屏幕启动了 MainActivity),StartMyServiceAtBootReceiver在 Android 3.1+ 上永远不会被调用。确保在尝试重新启动之前运行您的 Activity ,看看是否有帮助。

第三,您在 StartupManager 上实现了一个构造函数,这是个坏主意。请将此逻辑移至onCreate() .

第四,您的代码可能会在该构造函数中崩溃,如 getApplication()此时代码中不会返回有效值,特别是因为您未能链接到父类(super class)的构造函数。同样,将此代码移动到 onCreate()将在这里提供帮助。

第五,从onCreate()开始 Activity 服务的结构(更不用说它的构造函数)是非常不寻常的,用户可能不喜欢。此外,如果该服务没有做任何其他事情,您可以轻松地从 StartMyServiceAtBootReceiver 开始该 Activity 。并跳过 StartupManager完全。

第六,你有<intent-filter>您的服务上的元素,就好像您希望第三方开发人员调用这些服务一样。如果是这样,那很好。如果不是,请删除 <intent-filter>元素并使用显式 Intents在您的应用程序代码的其余部分中引用它们(例如 new Intent(this, StartupManager.class) ),以获得更好的安全性。或者,添加 android:exported="false"这些服务,尽管如果您删除 <intent-filter> 是自动的元素。

关于Android 应用程序在启动时未运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11801964/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com