Restart service on device reboot - java

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.

Related

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.

Parse sending notifications in dashboard but Android device not receiving

I've successfully created a Parse app and I'm using the application keys in my Android app implementation. I've followed all the steps here https://www.parse.com/apps/quickstart?onboard=#parse_push/android/existing (registration required unfortunately, but it's an instant process) and when I send a test push notification it is dispatched from the dashboard and is even received by a virtual device I'd set up as a control but nothing happens on a physical device. I thought it might be a socket issue so I alternated between wifi and 3G connections with no success
Here's the code I'm using:
Main:
package com.wetu.chronicle;
import android.os.Bundle;
import org.apache.cordova.*;
import com.parse.Parse;
import com.parse.ParseAnalytics;
import com.parse.ParseInstallation;
import com.parse.PushService;
public class chronicle extends DroidGap
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
super.loadUrl(Config.getStartUrl());
Parse.initialize(this, "xxxxxxxxx", "yyyyyyyyyyyy");
PushService.setDefaultPushCallback(this, chronicle.class);
PushService.subscribe(this, "Newsfeed", chronicle.class);
ParseInstallation.getCurrentInstallation().saveInBackground();
}
}
Android Manifest:
<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_LOCATION_EXTRA_COMMANDS" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.BROADCAST_STICKY" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<!--
IMPORTANT: Change "com.parse.starter.permission.C2D_MESSAGE" in the lines below
to match your app's package name + ".permission.C2D_MESSAGE".
-->
<permission android:protectionLevel="signature"
android:name="com.wetu.chronicle.permission.C2D_MESSAGE" />
<uses-permission android:name="com.wetu.chronicle.permission.C2D_MESSAGE" />
<application
android:debuggable="false"
android:hardwareAccelerated="true"
android:icon="#drawable/icon"
android:label="#string/app_name" tools:ignore="HardcodedDebugMode">
<activity android:name="chronicle" android:label="#string/app_name"
android:theme="#android:style/Theme.Black.NoTitleBar"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name="com.parse.PushService" />
<receiver android:name="com.parse.ParseBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.USER_PRESENT" />
</intent-filter>
</receiver>
<receiver android:name="com.parse.GcmBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<!--
IMPORTANT: Change "com.parse.starter" to match your app's package name.
-->
<category android:name="com.wetu.chronicle" />
</intent-filter>
</receiver>
</application>
<uses-sdk android:minSdkVersion="9" android:targetSdkVersion="19"/>
I'm available to clarify any questions you might have and/or post additional code.
Its most likely because your app isn't active in the background. A lot of my android apps have been struggling from this. A decent solution might be to make sure there is an alarm clock that wakes the app up. I've never dealt with parse though so this may not work, its worked for me in other scenarios though

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.

OnReceive call not working Android

I have a class which the code as follows:
public class CallManager extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
tManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
Log.i("onReceive","here");
}
}
Permissions on the manifest:
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.PROCESS_INCOMING_CALLS" />
<uses-permission android:name="android.permission.BOOT_COMPLETED" />
Receiver intent filter:
<receiver android:name="com.ram.tapdetector.CallManager">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
For some reason, nothing is getting logged from the onReceive function?
How can I fix this? Any help would be appreciated, thanks.
You need the following in your AndroidManifest.xml file:
1) In your element:
`<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />`
2) In your element (be sure to use a fully-qualified [or relative] class name for your BroadcastReceiver):
<receiver android:name="com.example.CallManager">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
`
In MyBroadcastReceiver.java:
` package com.example;
public class CallManager extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
tManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
Log.i("onReceive","here");
}
}`
if you are using HTC mobile-
Along with
`<action android:name="android.intent.action.BOOT_COMPLETED" /> `
also use,
`<action android:name="android.intent.action.QUICKBOOT_POWERON" />`
HTC devices dont seem to catch BOOT_COMPLETED
You have to add the permission
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
instead of
<uses-permission android:name="android.permission.BOOT_COMPLETED" />
for BOOT_COMPLETED. Checkout the documentation here.
As far as I see everything else should be ok.
p.s.: if you want to receive a broadcast event for incoming calls, use permission
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
and declare your intent filter with
<receiver
android:name="com.ram.tapdetector.CallManager"
android:enabled="true">
<intent-filter
android:priority="1000">
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
Check this nice tutorial for more.
Have you tried with flags:
android:enabled="true"
android:exported="true"
?
This question is outdated, but I have a suggest anyway.
Be carefull, you have twice times the permission :
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
This could generate some errors when you try to run your application.

BroadcastReceiver isn't receiving anything

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>

Categories

Resources