Can't I retrieve it again using Request ID?
like using:
pendingIntent = PendingIntent.getBroadcast(this, RequestID, intent, PendingIntent.FLAG_UPDATE_CURRENT);
PS: I have all pendingExtra's in PendingIntent, else I have to fetch from database again!
Yes, all PendingIntents will be deleted after you reboot. But you can recreate them when the phone is booted up. This tutorial here does a pretty good job of explaining how to deal with bootup listeners.
Define a class DeviceBootReceiver as follows, which will run your code (creating PendingIntents, or whatever else you want) when the device has been booted.:
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class DeviceBootReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
// Do your thing in here
}
}
}
And don't forget to (a) get permission and (b) declare this receiver in your AndroidManifest.xml:
<application ...>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
...
<receiver android:name=".DeviceBootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
</application>
Does all Pending Intent is deleted after boot in android?
Yes. They are only held in RAM, in a core OS process.
Can't I retrieve it again using Request ID?
Not after a reboot.
Related
I have an android service that runs when i open my app, now I want my android service to run at boot time. I tried the bellow code, but the service is not running automatically when i reboot my device. I cannot see it running as a service on my phone! Is there something wrong in my code?
I added these permissions to the manifest:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.RECEIVE_HEADSET_PLUG"/>
Here's my receiver in the manifest:
<receiver android:name="org.qtproject.example.MyBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.RECEIVE_BOOT_COMPLETED"/>
<action android:name="android.intent.action.RECEIVE_HEADSET_PLUG"/>
</intent-filter>
</receiver>
And here's MyBroadcastReceiver.java:
import android.os.Bundle;
import org.qtproject.qt5.android.bindings.QtActivity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class MyBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent startServiceIntent = new Intent(context, org.qtproject.example.MyCustomAppService.class);
context.startService(startServiceIntent);
}
}
replace
<action android:name="android.intent.action.RECEIVE_BOOT_COMPLETED"/>
with
<action android:name="android.intent.action.BOOT_COMPLETED"/>
in the manifest
ok so I had 2 problems in my code:
1)Thanks to #kajay, i had to change my action line as he described,
to be:
<action android:name="android.intent.action.BOOT_COMPLETED"/> in the manifest.
2) I was missing defining the package in the MyBroadcastReceiver.java. So, the class couldn't find the startServiceIntent. Of course qt doesn't give any errors or warnings with many java problems.
So, in my case i had to add this to the MyBroadcastReceiver.java :
package org.qtproject.example;
I had to do both of the above steps to fix my problem!
P.S Sometimes the service takes around 45 secs or more to start after booting!
In my Android application I want to run a Service without opening/running my application. For that I have extended BroadcastReciever class. But this BroadcastReceiver class is not being called from AndroidManifest.xml on BOOT_COMPLETE. So please tell what is the problem in my code? Or is there any other way to run a Service without opening my application? I have checked the control flow of my code and whole the code is working perfectly, the problem is that BroadcastReceiver is not being called.
Part of AndroidManifest.xml file:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<service android:name=".HelloService"
android:exported="false"/>
<receiver android:name=".MyBroadcastreceiver" android:enabled="true" android:exported="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
MyBroadcastreceiver.java class
package com.example.abc.project1;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class MyBroadcastreceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
/*this is not being called*/
Intent startServiceIntent = new Intent(context, HelloService.class);
context.startService(startServiceIntent);
}
}
Remove android:exported="false" from the <receiver>. That says you do not want anyone (other than yourself) sending a broadcast to this receiver. As a result, your receiver will be ignored by the system.
Beyond that, you also need an activity and to have run that activity before trying to reboot the device. You may already have that, but I thought that I would mention it for completeness.
I have an intent service in my application. When my application starts up it successfully runs the service class and it works nicely (storing data obtained from bluetooth in a sqlite database). The intent service is just for my app and will not be used by other apps.
However, when the application has been inactive for a long while, the service sometimes stops running. I want my service to carry on running reliably--that's why I created a service. I also want the service to start up by itself when the phone boots up (which it does not do).
When I go to settings -> applications -> running services my service is not listed there.
Here is the relevant part of my manifest file:
<service android:enabled="true" android:name=".MyHxMService" android:exported="false">
<intent-filter>
<action
android:name="org.xxxxx.MyHxMService" />
</intent-filter>
</service>
<receiver android:name="MyStartupIntentReceiver">
<intent-filter>
<action
android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.HOME" />
</intent-filter>
</receiver>
</application>
Here is my intent service class declaration:
public class MyHxMService extends IntentService {
Here is my MyStartupIntentReceiver:
package com.NewApp;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class MyStartupIntentReceiver extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
Intent serviceIntent = new Intent();
serviceIntent.setAction("org.xxxxx.MyHxMService");
context.startService(serviceIntent);
}
}
Start MyHxMService Service as
#Override
public void onReceive(Context context, Intent intent) {
Intent serviceIntent = new Intent(context,org.xxxxx.MyHxMService.class);
context.startService(serviceIntent);
}
Termination of service : IntentService is designed to be like an AsyncTask, it stops as soon as its work is done. If you need other behavior, consider extending the Service class itself. Also, returning START_STICKY from onStartCommand() in Service class tells android to keep it alive unless explicitly stopped.
Start Up at Boot: You have already setup the BOOT_COMPLETED IntentReciever, just create the Intent with proper Context and Component Class as shown in other answer.
Does every device send the BOOT_COMPLETED? I want to start an Activity on Boot Completed.
I put the following in the Manifest:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<receiver android:name=".BootFinished">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
Created the following class (receiver):
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.widget.Toast;
public class BootFinished extends BroadcastReceiver {
#Override
public void onReceive(Context mContext, Intent intent) {
if(intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
//do something like start an activity or service
}
try {
PackageManager pm = mContext.getPackageManager();
Intent launch = pm.getLaunchIntentForPackage("com.example.afterboot");
mContext.startActivity(launch);
} catch (Exception e) {
Toast.makeText(mContext, e.getMessage(), Toast.LENGTH_SHORT);
}
}
}
Am I missing something? Thanks!
Add full path and secondly add permission in your receiver.
<receiver android:name="com.example.BootFinished"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
As a starting point, try putting the full path of your receiver in the manifest.
The possible reasons why broadcast reciever events could fail:
Receiver not declared in AndroidManifest.xml
Declare the receiver in the Manifest-file:
Receiver in the Manifest xml is misspelled
Android-System is case sensitive. So check your spelling and path is correct in the AndroidMainfest.xml
AVD running for a long time
Reset your avd/device
4.Also if your app is in moved to sdcard.Say you have registered for android.intent.action.BOOT_COMPLETED,the boot event is triggered even before the mediascanner scans the sdcard.
and all the devices which run android sends BOOT_COMPLETE :P
Check again and try :)
all the best :)
I want to set an alarm via AlarmManager class. Normally, It works fine and everything is OK.
But if my application process stops or Phone turns off and turns on again, The alarm wont start. Any help?
or other service that android provides?
thanks
Android will erase all intent of alarm manager if you will restart your phone.
You have to create a receiver for start BOOT_COMPLETED and then you will get onReceive() method when your device will start.In this method you can create all alarm again.
You have to declare receiver in manifest
<receiver android:name=".MyStartupIntentReceiver" android:enabled="true" android:exported="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
And your receiver will be
import java.util.Calendar;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class MyStartupIntentReceiver extends BroadcastReceiver{
#Override
public void onReceive(final Context context, Intent intent1) {
// You can update pending intent here
}
}
For startup, try http://www.anddev.org/launch_activity_on_system-emulator_startup-t428.html
and for your process stop, try Android: How to auto-restart application after it's been "force closed"?