Question:
I have rewritten my question and this time I will be more specific. My problem is that I want to make my application start every time I start my phone. For this I have a java file called bootreceiver.java
that has 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 it in my launcheractivity or what steps I should take so that my bootreceiver turns on the application every time I turn on my mobile. As for the manifest, I understand it is just adding:
<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 in the manifest but I don't 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?
I have been testing on my phone and muchas veces falla y no se arranca la aplicación
there any way to solve 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, it is so that you can 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 have also tried with this and it has not worked:
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 , that 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 that you can IMPORT BOTH IN ANDROID AND ECLIPSE – The startup is set as a service, the question is that when I open my application is visible, but when I restart in the startup it never opens, but I open it. So the layout of my application should be visible when I turn on my mobile. If you put it as activity, it will return an error in the logcat as I quote.
Answer:
The error is as follows:
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):
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 start 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 start 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 don't need to start a service, you can directly start the main Activity of your application, therefore the BootReceiver
class 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 complete initialization (BOOT_COMPLETED) takes a few seconds after you see the device turned on, so do not despair.
I add acomplete example