In my Android project, I have a Service class:
public class MyService extends Service{
...
//Defined a WakefulBroadcastReceiver as a inner static class
public static class DeviceRebootReceiver extends WakefulBroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
//Do something when user reboot the device
}
}
}
As you see above, I have defined a WakefulBroadcastReceiver as a inner static class in MyService class. This receiver receives the broadcast when user reboot his/her device.
My AndroidManifest.xml looks like this:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.my.project"
android:versionCode="1"
android:versionName="2.1" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
...>
<activity ...> ... </activity>
<service android:name="com.my.project.MyService"
android:exported="false"/>
<receiver
android:name="com.my.project.MyService$DeviceRebootReceiver"
>
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
</manifest>
But when I run my app & after rebooted the device, my logcat shows me error:
Unable to instantiate receiver com.my.project.MyService$DeviceRebootReceiver:
java.lang.ClassNotFoundException: com.my.project.MyService$DeviceRebootReceiver in loader dalvik.system.PathClassLoader[/data/app/com.my.project-1.apk]
Why my receiver class is not able to be loaded by system?
=====Update======
I also tried to make my DeviceRebootReceiver as a separate class (DeviceRebootReceiver.java), and made the AndroidManifest.xml changed to :
<application...>
<receiver
android:name="com.my.project.DeviceRebootReceiver"
>
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
But I still get the similar error in logcat:
java.lang.RuntimeException: Unable to instantiate receiver com.my.project.DeviceRebootReceiver: java.lang.ClassNotFoundException
I had the same problem and solved it.
It's about the android-support-v4.jar library, you should only have the official and updated library (copied from ANDROID_SDK/extras/android/support/) under the libs folder in your project and Android Private Libraries checked in the Java Build Path of your project. For example, I also had a ClassNotFoundException because of my project was using the android-support-v4.jar of another library (ActionBarSherlock) even with the official android-support-v4.jar included in my java build path, so I unchecked the Android Private Libraries of the ActionBarSherlock project to avoid this redundancy.
Related
I created a simple sample to test the BroadcastReceiver reacting to the action BOOT_COMPLETED see. below, but it is not working. After starting the tablet is no activity / app is running and in the logocat is nothing. I'm probably a mistake in the settings, but I can not find out what
I use a tablet alps 874v3 android 4.4.2 and Visual Studio 2010 with Xamarin to write Android app in .net
On SO I found some additional information:
1 Registration BroadcastReceiver have not be inside AndroidManifest.xml but must use class attributes.
2 Applications must contain BroadcastReceiverand activity otherwise the will not run on later versions of Android (for safety)
3 Once installed the application is in stopped state so i started it (system verifies that the user wants the application) and then kill and then I try to reboot.
[BroadcastReceiver(Enabled = true, Exported = true, Permission = "RECEIVE_BOOT_COMPLETED")]
[IntentFilter(new string[] { "android.intent.action.BOOT_COMPLETED"})]
public class BootBroadcastReceiver : BroadcastReceiver
{
public BootBroadcastReceiver()
{
}
public override void OnReceive(Context context, Intent intent)
{
Log.Debug("TestBoot", "BootBroadcastReceiver.OnReceive()");
context.StartActivity(typeof(UsbMainActivity));
Log.Debug("TestBoot", "BootBroadcastReceiver.OnReceive() after start activity");
}
}
[Activity(Label = "UsbMainActivity", Icon = "#drawable/icon", MainLauncher = true, Permission = "RECEIVE_BOOT_COMPLETED")]
[IntentFilter(new string[] { "android.intent.action.BOOT_COMPLETED" })]
public class UsbMainActivity : Activity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
Utils.MyLog("TestBoot", 1, "UsbMainActivity.OnCreate()");
}
}
There is a AndroidMainfest.xml which was generated by xamarin :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="TestBoot.TestBoot" android:versionCode="1" android:versionName="1.0">
<uses-sdk android:minSdkVersion="15" android:targetSdkVersion="19" />
<application android:label="TestBoot" android:icon="#drawable/icon" android:name="mono.android.app.Application" android:debuggable="true">
<activity android:icon="#drawable/icon" android:label="UsbMainActivity" android:permission="RECEIVE_BOOT_COMPLETED" android:name="md5e98891b9b152ca725e5cab653b1387f3.UsbMainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</activity>
<receiver android:enabled="true" android:exported="true" android:permission="RECEIVE_BOOT_COMPLETED" android:name="md5e98891b9b152ca725e5cab653b1387f3.BootBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<provider android:name="mono.MonoRuntimeProvider" android:exported="false" android:initOrder="2147483647" android:authorities="TestBoot.TestBoot.mono.MonoRuntimeProvider.__mono_init__" />
<receiver android:name="mono.android.Seppuku">
<intent-filter>
<action android:name="mono.android.intent.action.SEPPUKU" />
<category android:name="mono.android.intent.category.SEPPUKU.TestBoot.TestBoot" />
</intent-filter>
</receiver>
</application>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
</manifest>
The problem was that after installing application i stopped via Settings- "Force Quit" and then tried to reboot. If I'm after installing stopped only a activity, so after the restart boot_completed arrived. So it seems that the application must running before the reboot and then the boot_completed arrived.
Note: Test reboot via adb console, type :
adb shell
am broadcast -a android.intent.action.BOOT_COMPLETED
add this permission to manifest
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
On some devices declaring the permission twice, once in the manifest and second as attribute to the receiver will make the receiver not working. You can leave only the permission declared inside the manifest. Check my comment (GeorgiZ) in this thread: https://forums.xamarin.com/discussion/80876/open-an-app-on-startup-after-booting-not-working.
I have implemented Broadcast Receiver in a library project for checking the Boot Completed event , but it is not working.
Broadcast Receiver Class :
public class Reciever extends BroadcastReceiver
{
public void onReceive(Context context, Intent intent)
{
if(intent.getAction().equals("android.intent.action.BOOT_COMPLETED"))
{
Toast.makeText(context, "Device Boot Completed", Toast.LENGTH_LONG).show();
}
}
}
AndroidManifest.xml :
<receiver
android:name=".Reciever"
android:enabled="true" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
I implemented same Receiver in another application (not library project) and it is working fine.
BroadcastReceiver cannot be defined in the library project's manifest. The hosting project always needs to declare the components.
android library project and Activities
Edit:
I feel this should work with latest Android studio/ gradle based projects.
You might skipped
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
In manifest file
Override the following method
#Override
public void onReceive(Context context, Intent intent)
You should add receive tag and reboot permissions to client application manifest file which using your library jar
Like this;
<receiver android:name="com.example.library.ReceiverClass"
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
I have a very simple application that works but when i add roboguice it throws
java.lang.RuntimeException: Unable to instantiate application
com.MyFirstApp.MyFirstApplication: java.lang.ClassNotFoundException:
com.MyFirstApp.MyFirstApplication
The application class:
public class MyFirstApplication extends RoboApplication {
#Override
protected void addApplicationModules(List<Module> modules) {
//modules.add(new DefaultModule());
}
}
The MainActivity:
public class MainActivity extends RoboActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
the manifest:
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.MyFirstApp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:name="MyFirstApplication">
<activity
android:name="com.MyFirstApp.Activities.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>
</application>
</manifest>
I have put guice-2.0-no_aop.jar and roboguice-1.1.3.jar in the assets folder and added them to the buildpath.
when i remove the robo part it works fine. can anyone tell me what i did wrong.
Well i found out what the problem was. i was using SDK v17+ and in that version external library's need te be placed in a "libs" folder and only in the libs folder. so all the tutorial's i found where they just put it in the assets folder where wrong.
Link to where i got the answer i was looking for: http://groups.google.com/group/roboguice/browse_thread/thread/474116b052050ae2
First of all, extending from RoboApplication is a RoboGuice 1.x thing, you should really be using RoboGuice 2.0 (where you no longer need to do that). Now, for your exception, is your Application class really at com.MyFirstApp.MyApplication? If not, you would need to update the name attribute to match.
I got an error in my android application when it tries to instantiate a receiver that i use to start a service on boot up. The error is obvious, it can not find the class file of my receiver. But everything is ok with my manifest file, the packages and all and i have no clue what is happening. Here is my code:
package dti.obd.reader;
import dti.obd.reader.service.MainService;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class BootReceiver extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
Intent serviceIntent = new Intent(MainService.class.getName());
context.startService(serviceIntent);
}
}
And my manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="dti.obd.reader"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<service android:name=".service.MainService" >
<intent-filter >
<action android:name="dti.obd.reader.service.MainService" />
</intent-filter>
</service>
<receiver android:name="dti.obd.reader.BootReceiver" >
<intent-filter >
<action android:name="android.intent.action.BOOT_COMPLETED" >
</action>
</intent-filter>
</receiver>
</application>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
</manifest>
Does anyone knows the erro? It seems that the package and the names are all ok...
You have to put your Receiver in some package. The system won't be able to instantiate it if it is on the main package.
I had the same problem. Fortunately before searching the error on internet I was doing another java project. I just realized that the bug in there was similar to this one. I tried it just now and worked. :)
I have also faced with this problem. Adding full package name to receiver definition in manifest file didn't help. Problem was there was an old odex file corresponding to my apk file. Android system loads classes from odex file so can not find receiver class.
Workarounds:
Remove the old odex file, or
Give a new name to your apk
http://www.addictivetips.com/mobile/what-is-odex-and-deodex-in-android-complete-guide/
try:
<receiver android:name=".BootReceiver" >
It adds the package name itself because you defined:
package="dti.obd.reader"
You have to put your Reciever in some package
Instead Add the full path of the Reciever
<receiver android:name="com.yourpackage.BootReceiver" >
It Sounds Weired but in my case it resolved the Issue
Hope Someone will be fruitful with this experience
I writed a small program to catch the system broadcast BOOT_COMPLETED, but it just doesn't work:
package com.alex.app.testsysreboot;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class MyReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.i("my_tag", "system reboot completed.......");
}
}
manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.alex.app.testsysreboot"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<application android:icon="#drawable/icon" android:label="#string/app_name">
<receiver android:name=".MyReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
</application>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
</manifest>
I closed the AVD, and then clicked the button "run" in Eclipse, and the Eclipse started a new AVD, but after the system boot, I just cannot see the log in the LogCat...
Well I tried this and it Works for me,
public class Autostart extends BroadcastReceiver
{
public void onReceive(Context arg0, Intent arg1)
{
Log.i("Autostart", "**********started************");
}
}
AndroidManifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="pack.saltriver" android:versionCode="1" android:versionName="1.0"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<application android:icon="#drawable/icon" android:label="#string/app_name">
<receiver android:name=".Autostart">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>
</manifest>
You need to add
android:enabled="true"
android:exported="true"
AND
make sure that the app is not installed on the SD card - IIRC apps installed there don't receive that BOOT_COMPLETED.
Another point is that devices with "Fast Boot" enabled (like several HTC devices) (sometimes?) don't send BOOT_COMPLETED.
Since Android 3.1+ there is some more weirdness regarding BOOT_COMPLETED relating to "very first start of an app" - see http://commonsware.com/blog/2011/07/13/boot-completed-regression-confirmed.html
A working sample project with source see https://github.com/commonsguy/cw-advandroid/tree/master/SystemEvents/OnBoot
From http://arthurfmay.blogspot.com/2011/06/broadcastreceiver-bootcompleted-and.html
So instead, from Eclipse I just went into the Android SDK and AVD
Manager (under the Window Menu) and started the emulator from there. I
did this of course after loading the app into the emulator. I start
the emulator and my BroadcastReceiver on boot works just fine. There
was no need to go to running the emulator at the command line.
Another working sample can be found here.