开机启动活动 [英] Start activity on boot

查看:196
本文介绍了开机启动活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想只需将手机开机后启动我的应用程序。显然,应用程序被启动后启动,但它会立即崩溃(仅仅是明确的应用程序正常工作正常)。我已阅读并尝试了不同的解决方案(<一href="http://stackoverflow.com/questions/10853879/start-an-activity-on-phone-boot-in-android">link1, <一href="http://stackoverflow.com/questions/14476335/application-is-not-starting-on-device-boot">link2)以及实际上是相同的code正常工作与其他应用程序我开发这里的code:

I'd like to start my app just after the phone boot. Apparently the app is started after the boot but it immediately crashes (just to be clear the app normally works fine). I have already read and tried different solutions (link1, link2) and actually the same code works fine with another app I was developing. Here's the code:

AndroidManifest.xml中:

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.bluetoothx10y"
    android:versionCode="1"
    android:versionName="1.0" >

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

    <uses-feature android:name="android.hardware.usb.accessory"/>
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.BLUETOOTH"/>
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
    <uses-permission android:name="android.permission.DISABLE_KEYGUARD"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >

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


        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:screenOrientation="landscape" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

            <intent-filter>
               <action android:name="android.hardware.usb.action.USB_ACCESSORY_ATTACHED"/>
            </intent-filter>

            <meta-data 
                android:name="android.hardware.usb.action.USB_ACCESSORY_ATTACHED"
                android:resource="@xml/accessory_filter">
            </meta-data>

        </activity>

        <activity android:name=".DeviceListActivity"
              android:label="@string/app_name"
              android:theme="@android:style/Theme.Dialog"
              android:screenOrientation="landscape" />        
    </application>

</manifest>

StartMyActivityAtBootReceiver.java:

StartMyActivityAtBootReceiver.java:

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

        if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {    

                Intent myStarterIntent = new Intent(context, MainActivity.class);
                myStarterIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(myStarterIntent);

            }    
    }   
}

难道相关的,我使用了大量的用户权限的事实?

Could it be related the fact that I'm using the a lot of user permissions?

推荐答案

我设法解决这个问题。里面的OnCreate()我有这个code(与USB通信),这是导致飞机失事的原因:

I managed to solve the problem. Inside the OnCreate() I had this code (related to the USB communication) which was causing the crash:

    act_string = getIntent().getAction();
    if( -1 != act_string.indexOf("android.intent.action.MAIN")){
        restorePreference();
    }           
    else if( -1 != act_string.indexOf("android.hardware.usb.action.USB_ACCESSORY_ATTACHED")){
        cleanPreference();
    }   

删除此code解决了开机启动后发出。

Deleting this code solved the start after boot issue.

这篇关于开机启动活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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