I'm trying to use startWakefulService(context, intent)
But after encoutering some issues I debugged my app and found that the intent being passed on to the method is false, it uses
Intent { act=com.google.android.c2dm.intent.RECEIVE flg=0x10 pkg=com.myApp.beta cmp=com.myApp.beta/fonctionnalite.Actualites.GcmReceiver (has extras) }
Instead of
Intent { act=com.google.android.c2dm.intent.RECEIVE flg=0x10 pkg=fonctionnalite.Actualites cmp=fonctionnalite.Actualites.GcmReceiver (has extras) }
So basically I'm trying to call the class called GcmReceiver which is in the package fonctionnalite.Actualites. I tried using a different component and intent but it still didn't work, would anyone know how I can solve this problem ?
Thanks !
Here is the code for the class using startWakefulService
public class GcmReceiver extends WakefulBroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
ComponentName comp = new ComponentName(context.getPackageName(), GcmMessageHandler.class.getName());
startWakefulService(context, (intent.setComponent(comp)));
setResultCode(Activity.RESULT_OK);
}
}
Related
Android Studio 3.2
public class GcmWakefulBroadcastReceiver extends WakefulBroadcastReceiver {
private static String TAG;
#Override
public void onReceive(Context context, Intent intent) {
ComponentName comp = new ComponentName(context.getPackageName(), GcmIntentService_.class.getName());
startWakefulService(context, (intent.setComponent(comp))); // crash here
setResultCode(Activity.RESULT_OK);
}
}
public class GcmIntentService extends IntentService {
private static String TAG = GcmIntentService.class.getName();
public GcmIntentService() {
super(TAG);
}
#Override
protected void onHandleIntent(Intent intent) {
}
}
On Android 8.0- success work. App is on background.
But on Android 8.0+ the app is crash with error:
FATAL EXCEPTION: main
Process: com.myproject.app, PID: 6506
java.lang.RuntimeException: Unable to start receiver com.myproject.app.gcm.GcmWakefulBroadcastReceiver: java.lang.IllegalStateException: Not allowed to start service Intent { act=com.google.android.c2dm.intent.RECEIVE flg=0x1000010 pkg=com.myproject.app cmp=com.myproject.app/.gcm.GcmIntentService_ (has extras) }: app is in background uid UidRecord{27efe68 u0a164 RCVR idle change:idle|uncached procs:1 seq(0,0,0)}
at android.app.ActivityThread.handleReceiver(ActivityThread.java:3194)
at android.app.ActivityThread.-wrap17(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1672)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.lang.IllegalStateException: Not allowed to start service Intent { act=com.google.android.c2dm.intent.RECEIVE flg=0x1000010 pkg=com.myproject.app cmp=com.myproject.app/.gcm.GcmIntentService_ (has extras) }: app is in background uid UidRecord{27efe68 u0a164 RCVR idle change:idle|uncached procs:1 seq(0,0,0)}
at android.app.ContextImpl.startServiceCommon(ContextImpl.java:1521)
at android.app.ContextImpl.startService(ContextImpl.java:1477)
at android.content.ContextWrapper.startService(ContextWrapper.java:650)
at android.content.ContextWrapper.startService(ContextWrapper.java:650)
at com.myproject.app.gcm.GcmWakefulBroadcastReceiver.onReceive(GcmWakefulBroadcastReceiver.java:44)
at android.app.ActivityThread.handleReceiver(ActivityThread.java:3187)
... 8 more
broadcast intent callback: result=CANCELLED
Crash in this line:
startWakefulService(context, (intent.setComponent(comp))); // crash here
My app must work on Android 4.4+
Use JobIntentService Instead of IntentService.
public class GcmIntentService extends JobIntentService {
private static String TAG = GcmIntentService.class.getName();
public static void startService(Context context) {
enqueueWork(context, GcmIntentService.class, 1001, new Intent());
}
#Override
protected void onHandleWork(Intent intent) {
}
}
And From your Receiver:
public class GcmWakefulBroadcastReceiver extends WakefulBroadcastReceiver {
private static String TAG;
#Override
public void onReceive(Context context, Intent intent) {
GcmIntentService.startService(context);
}
}
If you are using GcmWakefulBroadcastReceiver just for starting the service then, You do not need to use WakefulBroadcastReceiver.
When running on Android O, the JobScheduler will take care of wake
locks for you (holding a wake lock from the time you enqueue work
until the job has been dispatched and while it is running).
https://developer.android.com/reference/android/support/v4/app/JobIntentService
I want to open my android app as soon as notification is arrived without user interaction . But as my BroadcastReceiver cannot extend Activity so how I will I open another activity (as it requires Intent).
public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
ComponentName comp = new ComponentName(context.getPackageName(),
GCMNotificationIntentService.class.getName());
startWakefulService(context, (intent.setComponent(comp)));
setResultCode(Activity.RESULT_OK);
}
}
I am trying to start two services on reboot of the device using only one Broadcastreceiver. But only one service is called.
Here is my receiver:
public class FirstReciever extends BroadcastReceiver{
private static final String TAG8 = "Mytag";
#Override
public void onReceive(Context context, Intent intent) {
if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
Log.i(TAG8, "Restarting App after boot");
Intent myServiceInent = new Intent(context, FirstReciever.class);
context.startService(myServiceInent);
Intent myServiceInent1 = new Intent(context, DbService.class);
context.startService(myServiceInent1);
}
}
}
To start an Activity use:
startActivity(intent);
To Start a Service use:
startService(intent);
first of all look at this.
Intent myServiceInent = new Intent(context, FirstReciever.class);
context.startService(myServiceInent);
you have given the reference of your FirstReciever.class, that should be a service.
and if that doesn't work than start a single service and start another service from first one...
Hi I am looking a way to send any data in install intent & get this data in install/replace broadcast reciever.
I am doing install intent like below
Intent promptInstall = new Intent(Intent.ACTION_VIEW).setDataAndType(
Uri.parse("file:///" + Environment.getExternalStorageDirectory() + "/test.apk"), "application/vnd.android.package-archive");
promptInstall.putExtra("data", "value");
startActivity(promptInstall);
In Install Broadcast Receiver.
public class NewPackageReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.d("DEBUG"," test for application install/uninstall");
}
}
Question
I sent promptInstall.putExtra("data", "value"); in install intent then How I can get this data value in the install broadcast receiver.
Thanks in advance.
You can't. The extra data you add to your install Intent is not included in the PACKAGE_ADDED Intent sent by the system when a package was added.
What i did to achieved this is simply by creating a public method in the BroadcastReceiver, example :
public static void setAlarms(Context context)
And then i called that method in the actvitiy directly, and call it in the onReceive :
#Override
public void onReceive(Context context, Intent intent) {
setAlarms(context);
}
Is it possible to send a push notification to a user after they have left an android app for a defined period of time?
Yes of course!
You should suscribe your android to your GcmBroadcastReceiver class.
and add the permission
Your class will tell when a push message arrives, and be handled by your class in your app.
Here is an example:
public class GcmBroadcastReceiver extends WakefulBroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
// Explicitly specify that GcmIntentService will handle the intent.
ComponentName 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);
}
}