在启动Android应用程序没有运行 [英] Android Application not running at startup

查看:192
本文介绍了在启动Android应用程序没有运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是应该重新启动手机不断的设定次数基本的Andr​​oid应用程序。为了做到这一点,我需要在手机启动时启动的应用程序。为了这一点,我基本上是按照说明发现 href=\"http://stackoverflow.com/questions/6391902/how-to-start-an-application-on-startup\">,加入该权限清单和创建启动活动一个BroadcastReceiver类。下面是我的一些相关的code的:

I have a basic Android application that is supposed to reboot the phone continuously a set number of times. In order to do this, I need the application to be started upon phone startup. In order to that, I essentially followed the instructions found here, adding the permissions to the manifest and creating a BroadcastReceiver class that starts the activity. Here is some of my relevant code:

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);
    }
}

}

从清单文件:

<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的仿真器,应用程序似乎正常工作。也就是说,虽然我的模拟器的根源并非是和手机不执行重新启动命令正确,我也看到,在启动时,正确的活动开始了。

In the Eclipse emulator, the application appears to be working properly. That is, though my emulator isn't rooted and the phone doesn't execute the reboot commands correctly, I do see that, upon startup, the correct activity is started.

现在,当我尝试它运行Android 4.0.4在特定系统上,一切都在应用除正常启动时启动工作。有任何想法吗?

Now, when I'm trying it on a particular system running Android 4.0.4, everything in the application is working properly EXCEPT boot upon startup. Any ideas?

我试图通过安装在启动时启动,并证实它确实不开机启动时另一个应用程序,以消除任何硬件问题的可能性(因为我不使用市售发布的电话),而且它确实显示在启动后运行的应用程序作为高速缓存的过程。

I tried to eliminate the possibility of any hardware problems (since I'm not using a commercially-released phone) by installing another application that boots upon startup and confirming that it does indeed boot upon startup, and it does indeed show up under running apps as a cached process after startup.

我想AP preciate任何帮助。让我知道如果你需要任何额外的信息。

I would appreciate any help. Let me know if you need any additional information.

推荐答案

有一些问题在这里。

首先,你忘了张贴 StartMyServiceAtBootReceiver ,你期望获得在启动时控制部件,所以我们不能对是否有与它任何特别的问题发表评论。

First, you neglected to post StartMyServiceAtBootReceiver, the component you are expecting to get control at boot time, so we cannot comment on whether there are any particular problems with it.

二,除非有明确地执行你的组件之一(例如,用户推出的 MainActivity 从主屏幕), StartMyServiceAtBootReceiver 将永远不会被调用,在Android 3.1+。确保你试图在重新启动之前运行的活动,看看是否有帮助。

Second, unless something has explicitly executed one of your components (e.g., the user launched MainActivity from the home screen), StartMyServiceAtBootReceiver will never be invoked, on Android 3.1+. Make sure you run your activity before trying a reboot, and see if that helps.

第三,实施了 StartupManager 的构造,这是一个坏主意。请移动这个逻辑的onCreate()

Third, you implemented a constructor on StartupManager, which is a bad idea. Please move this logic to onCreate().

四,您的code可能会在构造函数中崩溃,如 getApplication()将不会在该code这点返回一个有效的值,特别是因为你没有链父类的构造函数。同样,这个移动code到的onCreate()将有助于在这里。

Fourth, your code will likely crash in that constructor, as getApplication() will not return a valid value at this point in the code, particularly since you failed to chain to the superclass' constructor. Again, moving this code to onCreate() will help here.

第五,从的onCreate开始一个活动()服务的(更不用说它的构造函数)是非常不寻常的,可能不是由用户pciated AP $ P $。此外,如果该服务是没有做别的,你可以很容易地启动该活动从 StartMyServiceAtBootReceiver 并跳过 StartupManager 完全。

Fifth, starting an activity from onCreate() of a service (let alone its constructor) is very unusual and may not be appreciated by the user. Moreover, if that service is not doing anything else, you could just as easily start that activity from StartMyServiceAtBootReceiver and skip StartupManager entirely.

第六,你有&LT;意向滤光器&gt;您的服务元素,如果你期待第三方开发者调用这些服务。如果是这样的情况下,细。如果没有,请删除&LT;意向滤光器&gt; 元素和使用明确的意图您的应用程序$其余内C $ C到引用它们(例如,新意图(这一点,StartupManager.class)),更好的安全性。或者,添加的android:出口=FALSE这些服务,但如果你删除是自动的&LT;意向滤光器&gt; 元素。

Sixth, you have <intent-filter> elements on your services, as if you are expecting third party developers to invoke those services. If that is the case, fine. If not, please remove the <intent-filter> elements and use explicit Intents within the rest of your app code to refer to them (e.g., new Intent(this, StartupManager.class)), for better security. Or, add android:exported="false" to those services, though that is automatic if you remove the <intent-filter> elements.

这篇关于在启动Android应用程序没有运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆