BroadcastReceiver isn't receiving anything - java

I would like to receive information in Application about Enable/Disable Location provider like NETWORK, GPS etc. I create simple BroadcastReceiver:
public class TestReciver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
Log.v("LOCATION_CHANGE_RECEIVER", "PROVIDER UPDATE");
}
}
And register it in AndroidManifest.xml
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<application
android:label="#string/app_name"
android:icon="#drawable/ic_launcher"
android:allowBackup="true"
android:debuggable="true">
<receiver android:name=".receiver.TestReciver">
<intent-filter>
<action android:name="android.intent.action.PROVIDER_CHANGED"/>
</intent-filter>
</receiver>
But when I try switch on/off GPS this BroadcastReceiver doesn't receive anything.

Change your intent filter to android.location.PROVIDERS_CHANGED
<receiver android:name=".receiver.TestReciver">
<intent-filter>
<action android:name="android.location.PROVIDERS_CHANGED" />
</intent-filter>
</receiver>

Related

Restart service on device reboot

I have a program with a service and if I reboot my device also the service should Restart. But this only works on the Emulator if I try this on my real device the service doesn't start at all.
Does someone know what I'm doing wrong or why it only works on Emulator?
BroadcastReviever:
#Override
public void onReceive(Context context, Intent intent) {
//we double check here for only boot complete event
if(intent.getAction().equalsIgnoreCase(Intent.ACTION_BOOT_COMPLETED))
{
//here we start the service
Intent serviceIntent = new Intent(context, UploadService.class);
context.startService(serviceIntent);
}
}
Manifest.xml:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.GET_ACCOUNTS_PRIVILEGED"
tools:ignore="ProtectedPermissions" />
<uses-permission android:name="android.permission.USE_CREDENTIALS" />
<uses-permission android:name="android.permission.READ_OWNER_DATA" />
<uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS"/>
<uses-permission android:name="android.permission.READ_OWNER_DATA" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="Upload FTP"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<receiver
android:name=".BootCompletedIntentReceiver"
android:enabled="true"
android:exported="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter >
</activity>
<service
android:name=".UploadService"
android:isolatedProcess="false"
android:exported="true"
android:enabled="true"/>
</application>
The emulator is at API 23 and real device is API 27.
I'm Building for a min API Level 23 and max API Level 27
EDIT
Now I have also tried the program with a emulator and android API 27 and there when I start my program and then I restart the emulator, the emulator doesn't start any more. As soon as the emulaor has started he begins to reboot again and that in a endless loop. (Real device starts normal just doesn't restarts service)
try to use LOCKED_BOOT_COMPLETED receiver as follow:
<receiver
android:name=".BootReceiver"
android:directBootAware="true"
android:exported="false">
<intent-filter>
<action android:name="android.intent.action.LOCKED_BOOT_COMPLETED"/>
<!-- For pre-N devices -->
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
don't forget permission
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
take a look here for more explanation
Now I fixed in on my own it was easier then i thought
public class BootCompletedIntentReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent i=new Intent(context, YourClass.class);
if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.O) {
context.startForegroundService(i);
}
else {
context.startService(i);
}
}
I had some weird experience if I don't put full URI,
for example .BootCompletedIntentReceiver would become com.company.BootCompletedIntentRecevier.
I know it sounds dumb but I had many weird regressions when not explicitly stating things on the Manifest with modern SDKs.

Android Studio: Application crashes at start

my application crashes at start and I have no idea why. I'm testing it on a real device (Samsung Galaxy S3 GT-I9300). Here is the exception:
EDIT:
So, that's what I've added to onCreate():
walking.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
travel_mode = "walking";
walking.setImageResource(R.drawable.walking_sel);
biking.setImageResource(R.drawable.regular_biking);
driving.setImageResource(R.drawable.car_icon);
transit.setImageResource(R.drawable.transit_icon);
}
});
biking.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
travel_mode = "biking";
walking.setImageResource(R.drawable.walking);
biking.setImageResource(R.drawable.regular_biking_sel);
driving.setImageResource(R.drawable.car_icon);
transit.setImageResource(R.drawable.transit_icon);
}
});
Before I've added this piece of code, everything worked correctly.
And here is AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="lukssoftware.kaart" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-library android:name="com.google.android.maps" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="***" />
<meta-data android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
<activity
android:name="lukssoftware.kaart.kaart_welcome"
android:screenOrientation="portrait"
android:label="kaart" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Any ideas?
Thanks.
Please post more code and there is nothing to do with manifest here post layout xml and activity code where you have defined walking and biking findViews().
Just check it out that you have defined findViewsById of both walking and biking before you are calling setOnClickListener method.
Does your application still compile successfully after deleting the two modules?
If it doesn't compile, look for the lines in your classes or xml files which have yellow or red exclamation marks. That should alert you to where the problem is coming from.
You should also take a good look at line 64 in your *welcome.java class.

GCM Android Client Not Firing WakefulService?

I have literally followed the examples for implementing GCM inside an android app and the app is receiving the messages using the following code:
public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// Explicitly specify that GcmIntentService will handle the intent.
ComponentName comp;
comp = new ComponentName(context.getPackageName(), GcmIntentService.class.getName());
// Start the service, keeping the device awake while it is launching.
startWakefulService(context, (intent.setComponent(comp)));
setResultCode(Activity.RESULT_OK);
}
}
The problem is that the GcmIntentService (public class GcmIntentService extends IntentService) is never being run? Am I missing something or should it usually fire the onHandleIntent method?
In case in any way related the Manifest file is below, thanks in advance:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.diversivi.test"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="19"/>
<application
android:label="#string/app_name"
android:icon="#drawable/ic_launcher">
<meta-data android:name="com.google.android.gms.version" android:value="#integer/google_play_services_version" />
<activity
android:name=".DemoActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<receiver
android:name=".GcmBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="com.diversivi.test" />
</intent-filter>
</receiver>
</application>
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<permission android:name="com.diversivi.test.permission.C2D_MESSAGE" android:protectionLevel="signature" />
<uses-permission android:name="com.diversivi.test.permission.C2D_MESSAGE" />
</manifest>
I don't see your GcmIntentService service declared in the manifest. That could be the problem.
In addition, the following line
comp = new ComponentName(context.getPackageName(), GcmIntentService.class.getName());
assumes that the GcmIntentService class is in the main package of your app (i.e. its full name is com.diversivi.test.GcmIntentService). If that's not the case, the broadcast receiver can't find the service class.

Application as global variable

i searched for way to store a global variables that will be shared with all the activities.
i found this:
How to declare global variables in Android?
and i created a class that extends application and i declare this application in the manifest file.
however i have a problem:
i cant decalre more than one application in the manifest file...
therefore if i want to declare two applications, what can i do?
here is my manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sa"
android:versionCode="1"
android:versionName="1.0" >
<uses-feature
android:glEsVersion="0x00020000"
android:required="true"/>
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="10" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.CAMERA"/>
<permission
android:name="com.example.sa.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<!-- The following two permissions are not required to use
Google Maps Android API v2, but are recommended. -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="com.example.sa.permission.MAPS_RECEIVE"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme"
android:debuggable="true"
android:name=".FriendPickerApplication">
<activity
android:name="com.example.sa.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>
<!-- Entry for RegisterActivity.class -->
<activity android:name=".RegisterActivity"
android:label="Register New Account"></activity>
<meta-data android:value="#string/app_id" android:name="com.facebook.sdk.ApplicationId"/>
<activity android:label="#string/app_name" android:name="com.facebook.LoginActivity"></activity>
<activity android:name=".ProfileActivity" android:label="Profile page"></activity>
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="xxxx"/>
<activity android:name="DashBoardActivity"></activity>
<activity android:name="AllItemsActivity"></activity>
<activity android:name="itemCategory"></activity>
<activity android:name="ItemMapActivity"></activity>
</application>
</manifest>
and i also want to delare a class that extends application that will store the shared variables:
<application android:name="MyApp"
android:icon="#drawable/icon"
android:label="#string/app_name2"></application>
and this is the class:
class MyApp extends Application {
private String myState;
public String getState(){
return myState;
}
public void setState(String s){
myState = s;
}
}
class Blah extends Activity {
#Override
public void onCreate(Bundle b){
...
MyApp appState = ((MyApp)getApplicationContext());
String state = appState.getState();
...
}
}
thanks alot
You can not declare more than one application.
You can have more variables in you application, but application is a singleton class.
To define Global Variable you can make use of static Keyword:
public static String myState;

Runtime error starting Service caused by ClassCastException

I am getting runtime error, trying to start a boot-time service. Its failing but there is no hint I can see. The intent is finding the Service but it doesn't get to onCreate or onStart.
public class ControlReceiver extends BroadcastReceiver
{
/**
* #see android.content.BroadcastReceiver#onReceive(Context,Intent)
*/
#Override
public void onReceive(Context context, Intent intent)
{
Intent service = new Intent(context, MyApp.class);
Log.d("rcvr", "Received intent:" + intent.getAction());
context.startService(service);
}
}
The manifest:
<manifest
android:versionCode="1"
android:versionName="1.0"
package="com.company.package"
xmlns:android="http://schemas.android.com/apk/res/android">
<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="10"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_GPS"/>
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_ASSISTED_GPS"/>
<uses-permission android:name="android.permission.ACCESS_CELL_ID"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_LOGS"/>
<uses-permission android:name="android.permission.SET_ACTIVITY_WATCHER"/>
<uses-permission android:name="android.permission.STATUS_BAR"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.BROADCAST_STICKY"/>
<uses-permission android:name="android.permission.CONTROL_LOCATION_UPDATES"/>
<uses-permission android:name="android.permission.SET_DEBUG_APP"/>
<uses-permission android:name="android.permission.BATTERY_STATS"/>
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS"/>
<uses-permission android:name="android.permission.BIND_DEVICE_ADMIN"/>
<uses-permission android:name="android.permission.DIAGNOSTIC"/>
<uses-permission android:name="android.permission.DEVICE_POWER"/>
<uses-permission android:name="android.permission.DUMP"/>
<uses-permission android:name="android.permission.CHANGE_COMPONENT_ENABLED_STATE"/>
<uses-permission android:name="android.permission.SIGNAL_PERSISTENT_PROCESSES"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.PERSISTENT_ACTIVITY"/>
<uses-permission android:name="android.permission.HARDWARE_TEST"/>
<uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES"/>
<application
android:icon="#drawable/ic_launcher"
android:label="#string/mod_name"
android:name=".MyApp">
<!-- We declare our service here -->
<service
android:enabled="true"
android:exported="true"
android:icon="#drawable/ic_launcher"
android:label="#string/serv_name"
android:name=".MyApp"
android:process=":my_process">
<intent-filter>
<action android:name="com.company.package.MY_INTENT" />
</intent-filter>
</service>
<receiver
android:name=".ControlReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="android.intent.action.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE" />
</intent-filter>
</receiver>
<receiver
android:name=".BatteryHelper" >
<intent-filter>
<action android:name="android.intent.action.BATTERY_OKAY"/>
<action android:name="android.intent.action.BATTERY_CHANGED"/>
<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED"/>
<action android:name="android.intent.action.ACTION_POWER_CONNECTED"/>
<action android:name="android.intent.action.BATTERY_LOW"/>
<category android:name="android.intent.category.INFO"/>
</intent-filter>
</receiver>
</application>
</manifest>
The exception thrown
FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to instantiate application com.company.package.MyApp: java.lang.ClassCastException: com.company.package.MyApp cannot be cast to android.app.Application
at android.app.LoadedApk.makeApplication(LoadedApk.java:529)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4442)
at android.app.ActivityThread.access$1300(ActivityThread.java:139)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1305)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:4945)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
Update
When I removed android:name=".MyApp" for the application from the manifest, the service started. Anyone know why?
when I removed android:name=".MyApp" for the application from the manifest, the service started. Anyone know why?
The reason for this is most likely the fact that your MyApp class does not extend android.app.Application.
As the Android Developer Site says:
android:name
The fully qualified name of an Application subclass
implemented for the application. When the application process is
started, this class is instantiated before any of the application's
components.
The subclass is optional; most applications won't need
one. In the absence of a subclass, Android uses an instance of the
base Application class.
Since subclassing Application is optional, removing android:name=".MyApp" allows your app to properly start because you're no longer trying to cast MyApp to Application which is exactly what your exception claims your app is trying to do.

Categories

Resources