0

I m developing an android application which should start in the boot of android.

For that, I added the following line in the AndroidManifest.xml

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

and

<receiver android:name=".AppBroadcastReceiver">
    <intent-filter >
        <action android:name="android.intent.action.BOOT_COMPLETED"/>
    </intent-filter>
</receiver>

And I created a new file (new class):

public class AppBroadCastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        if(intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)){
            Intent i = new Intent(context, MyService.class);
            i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startService(i);
        }
    }

}

But when I boot my android system, I remarked that my application start but it directelly crashs

How to fix these problems?

EDIT

Here after the crash logs:

12-26 16:47:30.791: E/AndroidRuntime(1877): FATAL EXCEPTION: main
12-26 16:47:30.791: E/AndroidRuntime(1877): java.lang.RuntimeException: Unable to instantiate receiver com.example.openafreerdp.receiver.AppBroadcastReceiver: java.lang.ClassNotFoundException: com.example.openafreerdp.receiver.AppBroadcastReceiver
12-26 16:47:30.791: E/AndroidRuntime(1877):     at android.app.ActivityThread.handleReceiver(ActivityThread.java:2337)
12-26 16:47:30.791: E/AndroidRuntime(1877):     at android.app.ActivityThread.access$1500(ActivityThread.java:149)
12-26 16:47:30.791: E/AndroidRuntime(1877):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1376)
12-26 16:47:30.791: E/AndroidRuntime(1877):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-26 16:47:30.791: E/AndroidRuntime(1877):     at android.os.Looper.loop(Looper.java:153)
12-26 16:47:30.791: E/AndroidRuntime(1877):     at android.app.ActivityThread.main(ActivityThread.java:4987)
12-26 16:47:30.791: E/AndroidRuntime(1877):     at java.lang.reflect.Method.invokeNative(Native Method)
12-26 16:47:30.791: E/AndroidRuntime(1877):     at java.lang.reflect.Method.invoke(Method.java:511)
12-26 16:47:30.791: E/AndroidRuntime(1877):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)
12-26 16:47:30.791: E/AndroidRuntime(1877):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
12-26 16:47:30.791: E/AndroidRuntime(1877):     at dalvik.system.NativeStart.main(Native Method)
12-26 16:47:30.791: E/AndroidRuntime(1877): Caused by: java.lang.ClassNotFoundException: com.example.openafreerdp.receiver.AppBroadcastReceiver
12-26 16:47:30.791: E/AndroidRuntime(1877):     at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
12-26 16:47:30.791: E/AndroidRuntime(1877):     at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
12-26 16:47:30.791: E/AndroidRuntime(1877):     at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
12-26 16:47:30.791: E/AndroidRuntime(1877):     at android.app.ActivityThread.handleReceiver(ActivityThread.java:2332)
12-26 16:47:30.791: E/AndroidRuntime(1877):     ... 10 more

EDIT 2: The manifest xml file:

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

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

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.openafreerdp.MainActivity"
            android:excludeFromRecents="true"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service android:enabled="true" android:name=".UnboundedService" />
        <receiver
            android:name="com.example.openafreerdp.AppBroadcastReceiver"
            android:enabled="true"
            android:exported="true"
            android:label="AppBroadcastReceiver"
            android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >
            <intent-filter>
               <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>
    </application>

</manifest>
MOHAMED
  • 41,599
  • 58
  • 163
  • 268

1 Answers1

1

There is a typo in your code. Try to replace following code

<receiver android:name=".AppBroadcastReceiver">

with

<receiver android:name=".AppBroadCastReceiver">

For your information, touching on the screen is nothing to do with starting your service when the system finished booting.

UPDATED:

<receiver android:name="com.example.openafreerdp.receiver.AppBroadCastReceiver">
sam
  • 2,780
  • 1
  • 17
  • 30