Pergunta

I am trying to get my app to auto start on boot up and it will and an error occurs while launching the application

Here is my manifest and the program file for the "Auto Starter":

manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="this.bad.file"
android:versionCode="1"
android:versionName="1.0" >

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

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

<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" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <receiver
        android:name="autoBot"
        android:enabled="true"
        android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>
</application>

</manifest> 

Here is the "AutoBot" as I have called it (Not for spammy reasons I just like transformers):

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class autoBot extends BroadcastReceiver {
    public void onReceive(Context context, Intent intent) {
        Intent startUp = new Intent(context, MainActivity.class);
        context.startActivity(startUp);
    }
}  

So there we have it!

Foi útil?

Solução

In your manifest, you left out a vital name of the class, usually, can be abbreviated to [dot][ClassName] or the full package name, as in for example .autoBot or see the example below:

<receiver android:name="this.bad.file.autoBot">
    <intent-filter>
       <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
</receiver>

And in your Broadcast receiver:

public class autoBot extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Intent startUp = new Intent(context, MainActivity.class);
        context.startActivity(startUp);
    }
}  

Notice the usage of @Override on the onReceive class.

The normal recommended route to take is usually, to start an alarm on boot, and listen for the broadcast when the alarm has expired, in that way your activity does not hog up the boot and also, allow the boot up process to "settle" for a bit.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top