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

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

问题描述

我有一个基本的Android应用程序,应该连续重启手机一定次数。为了做到这一点,我需要通过电话启动启动应用程序。为此,我基本上遵循这里的说明,添加清单的权限,并创建启动该活动的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);
}
}

}



从清单文件中:

 < 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仿真器中,应用程序似乎正常工作。也就是说,虽然我的模拟器没有rooted,手机没有正确执行重新启动命令,但我确实发现,在启动时,正确的活动开始。



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



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



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

解决方案

这里有一些问题。



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



第二,除非有明确的执行其中一个组件(例如,用户启动 MainActivity 从主屏幕),在Android 3.1+上,将永远不会调用 StartMyServiceAtBootReceiver 。确保您在尝试重新启动之前运行您的活动,并查看是否有帮助。



第三,您在 StartupManager ,这是一个坏主意。请将此逻辑移至 onCreate()



第四,您的代码可能会在该构造函数中崩溃, code> getApplication()在代码中的这一点上不会返回一个有效的值,特别是因为你没有链接到超类的构造函数。再次,将此代码移动到 onCreate()将有助于此。



第五,从<$ c开始一个活动$ c> onCreate()的服务(更不用说它的构造函数)是非常不寻常的,可能不被用户赞赏。此外,如果该服务没有做任何其他事情,您可以从 StartMyServiceAtBootReceiver 轻松启动该活动,并跳过 StartupManager



第六,您的服务中有< intent-filter> 元素,就像您期待的一样第三方开发人员调用这些服务。如果是这样,罚款。如果没有,请删除< intent-filter> 元素,并在其余的应用程序代码中使用显式的 Intents 引用它们(例如, new Intent(this,StartupManager.class)),以提高安全性。或者,将 android:exported =false添加到这些服务中,但如果删除< intent-filter> 元素。


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

}

From the manifest file:

<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>

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.

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.

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

解决方案

There are a number of issues here.

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.

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.

Third, you implemented a constructor on StartupManager, which is a bad idea. Please move this logic to 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.

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.

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天全站免登陆