Start an application programmed in java for android in the startup

8

I have rewritten my question and this time I will be more specific. My problem is that I want to start my application every time I start my phone. For this I have a java file called bootreceiver.java that I have the following:

package my.app.client;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.preference.PreferenceManager;
import android.util.Log;

public class BootReceiver extends BroadcastReceiver {

    public final String TAG = BootReceiver.class.getSimpleName();

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i(TAG, "BOOT Complete received by Client !");

        String action = intent.getAction();

        if (action.equals(Intent.ACTION_BOOT_COMPLETED)) {

            Intent serviceIntent = new Intent(context, Client.class);
            serviceIntent.setAction(BootReceiver.class.getSimpleName());
            context.startService(serviceIntent);

        }
    }

}

So what I need to know is I have to tell you in my launcheractivity or what steps I should take for my bootreceiver to turn on the application every time I turn on my mobile. As for the manifest thing I understand it is just add:

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

<receiver android:name="my.app.client.BootReceiver">
   <intent-filter>
      <action android:name="android.intent.action.BOOT_COMPLETED"/>
   </intent-filter>
</receiver>

In my case the name of my class is BootReceiver so I would have to put:

<receiver android:name="BootReceiver">

This I will put it in the manifest but I do not know what I have to put in my launcheractivity or what I have to do with bootreceiver I just have to add it to the manifest or do I have to do something else?

Have I been testing on my phone and muchas veces falla y no se arranca la aplicación is there any way to fix it?

logcat:

01-01 00:04:02.360: E/Trace(1464): error opening trace file: No such file or directory (2)
01-01 00:04:02.410: W/asset(1464): Asset path /data/app/my.app.client-1.apk is neither a directory nor file (type=1).
01-01 00:04:02.780: W/asset(1464): Asset path /data/app/my.app.client-1.apk is neither a directory nor file (type=1).
01-01 00:04:02.820: D/AndroidRuntime(1464): Shutting down VM
01-01 00:04:02.820: W/dalvikvm(1464): threadid=1: thread exiting with uncaught exception (group=0x40acd930)
01-01 00:04:02.830: E/AndroidRuntime(1464): FATAL EXCEPTION: main
01-01 00:04:02.830: E/AndroidRuntime(1464): java.lang.RuntimeException: Unable to instantiate receiver my.app.client.receiver.BootReceiver: java.lang.ClassNotFoundException: Didn't find class "my.app.client.receiver.BootReceiver" on path: /data/app/my.app.client-1.apk
01-01 00:04:02.830: E/AndroidRuntime(1464):     at android.app.ActivityThread.handleReceiver(ActivityThread.java:2357)
01-01 00:04:02.830: E/AndroidRuntime(1464):     at android.app.ActivityThread.access$1500(ActivityThread.java:141)
01-01 00:04:02.830: E/AndroidRuntime(1464):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1310)
01-01 00:04:02.830: E/AndroidRuntime(1464):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-01 00:04:02.830: E/AndroidRuntime(1464):     at android.os.Looper.loop(Looper.java:137)
01-01 00:04:02.830: E/AndroidRuntime(1464):     at android.app.ActivityThread.main(ActivityThread.java:5041)
01-01 00:04:02.830: E/AndroidRuntime(1464):     at java.lang.reflect.Method.invokeNative(Native Method)
01-01 00:04:02.830: E/AndroidRuntime(1464):     at java.lang.reflect.Method.invoke(Method.java:511)
01-01 00:04:02.830: E/AndroidRuntime(1464):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-01 00:04:02.830: E/AndroidRuntime(1464):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-01 00:04:02.830: E/AndroidRuntime(1464):     at dalvik.system.NativeStart.main(Native Method)
01-01 00:04:02.830: E/AndroidRuntime(1464): Caused by: java.lang.ClassNotFoundException: Didn't find class "my.app.client.receiver.BootReceiver" on path: /data/app/my.app.client-1.apk
01-01 00:04:02.830: E/AndroidRuntime(1464):     at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:65)
01-01 00:04:02.830: E/AndroidRuntime(1464):     at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
01-01 00:04:02.830: E/AndroidRuntime(1464):     at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
01-01 00:04:02.830: E/AndroidRuntime(1464):     at android.app.ActivityThread.handleReceiver(ActivityThread.java:2352)
01-01 00:04:02.830: E/AndroidRuntime(1464):     ... 10 more
01-01 00:05:47.000: I/Process(1464): Sending signal. PID: 1464 SIG: 9

If I reformulate the question is for you to explain to me that I am doing wrong from the logcat. Because it shows me not found if in my manifest I have my class ..

I also tried this and it did not work:

add the BootReceiver class inside another package, for example:

<receiver android:name="my.app.client.receiver.BootReceiver">
   <intent-filter>
      <action android:name="android.intent.action.BOOT_COMPLETED"/>
   </intent-filter>
</receiver>

The project is , which I modify for educational purposes:

This is the project to import, click here

The application is called ZMaster Clean Pro (The application appears at the end), it is a client application, it is in a .zip file so you can IMPORT SO MUCH IN ANDROID AS ECLIPSE - The startup is put as a service, the issue is that when I open my application it is visible, but when I restart the startup it never opens, but I open it. Therefore, the layout of my application should be visible when I turn on my mobile phone. If you put it as an activity it will return an error in the logcat as cite.

    
asked by Sergio Ramos 02.06.2017 в 17:32
source

3 answers

9

The error is as follows:

  

Caused by: java.lang.ClassNotFoundException: Did not find class   "my.app.client.receiver.BootReceiver" on path:   /data/app/my.app.client-1.apk 01-01 00: 04: 02.830:   E / AndroidRuntime (1464):

You must define the correct package and class in the receiver, apparently it should be:

<receiver android:name="my.app.client.BootReceiver">

In order for your application to run when you fully launch your device (BOOT_COMPLETED), it is important to add the permission:

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

Enable the receiver and add the corresponding intent-filter to start the application when you fully launch your device:

<receiver android:enabled="true" android:name=".BootReceiver"
        android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
   <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
            <category android:name="android.intent.category.DEFAULT" />
   </intent-filter>
</receiver>

If you define the execution of a BroadcastReceiver from your AndroidManifest.xml , you do not need to start a service, you can directly initiate the main Activity of your application, therefore the class BootReceiver would be:

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

public class BootReceiver extends BroadcastReceiver {

    private static final String TAG = "BootUpReceiver";

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i(TAG,  "* onReceive()");
        Intent i = new Intent(context, MainActivity.class);
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(i);
    }
}

Remember that the full initialization (BOOT_COMPLETED), takes a few seconds after you see the device turned on, so do not despair.

I add a full example

    
answered by 02.06.2017 в 18:12
3

The AndroidManifest is simple what it has to have, the permission to execute it when you turn on the phone and the receiver:

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

and the definition of the receiver:

<receiver
        android:name=".BootReceiver"
        android:enabled="true"
        android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
            <category android:name="android.intent.category.DEFAULT" /> //Esto lo he usado para un servicio, no se si te hará falta para arrancar una aplicación
        </intent-filter>
    </receiver>

Notice that in android: name of the receiver starts with a period (.BootReceiver) Now you just have to start the activity from the BootReceiver class as indicated by @JorgeSys.

You have to make sure that you can start the Client activity from a BroadcastReceiver, because it is easy to give you a bug if you have not prepared it well (if you try to collect data from the intent or something like that), to check it shows a message with a Toast when starting the client or something you can see if the failure is in the BootReceiver or in the Client class.

    
answered by 09.06.2017 в 10:48
3
  

First of all, consider the following Important Note:

     

Due to security reasons, Android will not boot   automatic application without having launched it before   manual at least once . After executing it manually by   the first time, the application can start automatically every time   that the system starts.

Now, let's review the matter step by step:

When the Android system starts, a full boot event is sent. You can listen to and capture this event to take specific actions, such as the automatic start of an activity or service.

As already mentioned in other answers, some permissions are necessary.

If you follow these steps, it should work:

Step 1

In the AndroidManifest.xml give the application the permission android.permission.RECEIVE_BOOT_COMPLETED just before the node application :

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

Step 2

In the same AndroidManifest.xml , within the node application define the Activity that will start automatically when Android starts.

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

Step 3

Always in the AndroidManifest.xml , within the node application , register the listener of the Reciever for the event of full load (boot complete event):

<receiver
    android:name=".MyBootReceiver"
    android:label="IniciarMyBootReceiver">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
</receiver>

Step 4

Create the receiving class that will listen to the completed boot event. This class can extend the abstract class BroadcastReceiver , whose onReceive() method will be called when the device startup is complete. It would also be important that this class is in the same package as the Main class .

public class MyBootReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
            Intent activityIntent = new Intent(context, MainActivity.class);
            activityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(activityIntent);
        }
    }
}

For more details, you can consult this link (in English): Auto-start custom Android applications     
answered by 13.06.2017 в 15:42