If you go to Settings->About Phone->Check for updates a check is initiated to see if theres any system updates ready for your phone.
How can I do this action programmatically? Further, I am trying to locate where in the Android source code this happens so I can see it fully and understand it better. Does anyone have any suggestions?
As far as I know, there is no known broadcast, intent or API to do this programmatically.
And it depends on the ROM, and manufacturer.
Sony for example uses a service which, when the wifi is activated, the service checks on Sony's servers for any updates and informs of it.
But when talking about AOSP source, that I do not think happens.
The nearest point of System update is found in packages/apps/Settings/src/com/android/settings/DeviceInfoSettings.java
Protip: grep the string "System update" within the res/values directory and work backwords and find out where that string variable identifier is used!
Edit:
Here's an example broadcast receiver:
public class SystemUpdateClass extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent){
if (intent.getAction().equals("android.settings.SYSTEM_UPDATE_SETTINGS")){
Toast.makeText(context,
"Yup! Received a system update broadcast",
Toast.LENGTH_SHORT).show();
}
}
}
Here's an example code, from within a activity's onCreate:
SystemUpdateClass sysUpdate = new SystemUpdateClass();
IntentFilter filter = new IntentFilter();
filter.addAction("android.settings.SYSTEM_UPDATE_SETTINGS");
registerReceiver(sysUpdate, filter);
Now, what should happen is your app should receive the broadcast, unless I am mistaken that the broadcast is only for system-signed apps... however the rest is left as an exercise :)
Related
To learn something new, I'm developing an Android APP (min SDK version API 23 and Target SDK Version API 28) that allows me and my family to create and share a virtual shopping list through HTTP requests and JSON responses on a free Web. Everything works fine, but I want to add a feature: I would like to get notified when someone makes a change even when the app is killed or has never been launched. I know what the task could do to compare the changes made on the list and I also know that it is something to be done once every 5 minute (for example), but I don't know how to perform background operations when the app is no longer running and it has been killed from the recent tasks list. I gave the Service class a try, but when the app is killed it stops. So I looked for a solution and I found the BroadcastReceiver and made it able to receive a message whenever the Service stops in order to restart it. But from Android API 26 the BroadcastReceiver must be (I guess..) contex-registered.
So this is what I my main Activity does when the onCreate method is called:
ReceiverCall receiver = new ReceiverCall();
registerReceiver(receiver, new IntentFilter("com.dadex.familyapp.startServiceRequest"));
My ReceiverCall which extends the BroadcastReceiver Class:
public class ReceiverCall extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
try{
String action = intent.getAction();
if (action.equals("com.dadex.familyapp.startServiceRequest"))
context.startService(new Intent(context, CheckListService.class));
}
catch (Exception e){
Log.e("ERROR", e.getMessage());
}
}
}
And this is my CheckListService onDestroy method:
#Override
public void onDestroy() {
Intent intent = new Intent("com.dadex.familyapp.startServiceRequest");
sendBroadcast(intent);
}
It works fine when the app is launched, but as soon as I kill it, the receiver won't receive anything. So my question is: what is the best way to perform such background operations? Are there other classes I need to learn first? Thanks a lot!
You need a background service, with a notification to keep it alive.
startForegorund()
Search for startForegorund with notification and you will find what you need.
In Android 5.0 onwards, HidService.java includes following function:
private void broadcastReport(BluetoothDevice device, byte[] report, int rpt_size) {
Intent intent = new Intent(BluetoothInputDevice.ACTION_REPORT);
intent.putExtra(BluetoothDevice.EXTRA_DEVICE, device);
intent.putExtra(BluetoothInputDevice.EXTRA_REPORT, report);
intent.putExtra(BluetoothInputDevice.EXTRA_REPORT_BUFFER_SIZE, rpt_size);
intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
sendBroadcast(intent, BLUETOOTH_PERM);
}
I am not able to find any documentation on this flag in the intent. How should I receive this broadcast intent in my app?
==============
Edited content deleted and formed into new question here
This constant is not documented in the Intent API docs because it is not intended for public use.
Here is a description from the android source code I found that describes it. (line 3018)
FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT
public static final int FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT
If set, when sending a broadcast before boot has completed only registered receivers will be called -- no BroadcastReceiver components will be launched. Sticky intent state will be recorded properly even if no receivers wind up being called. If FLAG_RECEIVER_REGISTERED_ONLY is specified in the broadcast intent, this flag is unnecessary.
This flag is only for use by system sevices as a convenience to avoid having to implement a more complex mechanism around detection of boot completion.
Emphasis mine.
I have a situation where I only want to register a BroadcastReceiver for Time changed on certain platforms. For performance reasons, I would like to register the receiver dynamically via the context.registerReceiver method only on the platforms needed. I can detect the platform on app first run and decide if I would like to listen to this event.
While testing this approach I have found that when registering via context.registerReceiver the application will not be woken up if the process is not running.
Is this expected and is there a setting to get around it?
IntentFilter filter = new IntentFilter(Intent.ACTION_TIME_CHANGED);
this.getApplicationContext().registerReceiver(new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Log.e("MYTAG", "Received Broadcast from dyn one");
}
}, filter);
When your broadcast registered dynamically you can receive messages only when your application is running. If you want it to wake up you need to declare your broadcast in manifest
A dynamically registered receiver has a short lifetime so it wont wake up your device always. You have to declare it in your Manifest if you want it to behave they way you need!
P.S.: Had exactly the same problem recently so i am pretty sure for this!
I have the following code to receive SMS by my application:
public class SMSReceiver extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
...
In order to test it, I have to send an SMS with telnet (and I can do nothing with Android x86).
Can I call this onReceive programmatically for ex., when button is pressed or application is started? What kind of parameters should I pass (i.e. where tel number should be? text of the message etc.)?
Upd. I found this answer - https://stackoverflow.com/a/12338541/604388. If I follow that code, i.e.:
intent.setClassName("com.android.mms",
"com.android.mms.transaction.SmsReceiverService");
context.startService(intent);
then standard event is fired (i.e. message is received by standard android Messaging app), but my code at onReceive doesn't work.
If I replace it with:
// intent.setClassName("com.android.mms",
// "com.android.mms.transaction.SmsReceiverService");
context.sendBroadcast(intent);
then my code at onReceive works, but not standard android app.
How can I fix it?
Does this help? DDMS can emulate SMS messages, calles and locations. It's integrated with Eclipse set up for Android development and Android studio. You can find it at:
Window > Open Perspective > Other... > DDMS.
http://developer.android.com/tools/debugging/ddms.html
I'm trying to get a BroadcastReceiver invoked when the screen is turned on. In my AndroidManifest.xml I have specified :
<receiver android:name="IntentReceiver">
<intent-filter>
<action android:name="android.intent.action.SCREEN_ON"></action>
</intent-filter>
</receiver>
However it seems the receiver is never invoked (breakpoints don't fire, log statements ignored). I've swapped out SCREEN_ON for BOOT_COMPLETED for a test, and this does get invoked.
This is in a 1.6 (SDK level 4) project.
A Google Code Search revealed this, I downloaded the project and synced it, converted it to work with latest tools, but it too is not able to intercept that event.
http://www.google.com/codesearch/p?hl=en#_8L9bayv7qE/trunk/phxandroid-intent-query/AndroidManifest.xml&q=android.intent.action.SCREEN_ON
Is this perhaps no longer supported?
Previously I have been able to intercept this event successfully with a call to Context.registerReceiver() like so
registerReceiver(new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
// ...
}
}, new IntentFilter(Intent.ACTION_SCREEN_ON));
However this was performed by a long-living Service. Following sage advice from CommonsWare I have elected to try to remove the long-living Service and use different techniques. But I still need to detect the screen off and on events.
Following sage advice from CommonsWare
I have elected to try to remove the
long-living Service and use different
techniques.
Actually, I believe my advice was more of a light blue... :-)
But I still need to detect the screen
off and on events.
There are certain events that Android does not want to start up new processes for, so the device does not get too slow from all sorts of stuff all having to run at once. ACTION_SCREEN_ON is one of those. See this previous question for light blue advice on that topic.
So, you need to ask yourself, "Self, do I really need to get control on those events?". The core Android team would like it if your answer was "no".
This is the best example I've found http://androidexample.com/Screen_Wake_Sleep_Event_Listner_Service_-_Android_Example/index.php?view=article_discription&aid=91&aaid=115
Actullay i was faceing this issue but i resolve it succeessfully
1) start service from your main activity
Intent i = new Intent(MainActivity.this, UpdateService.class);
startService(i);
2) register reciver in service class.
#Override
public void onCreate() {
super.onCreate();
// REGISTER RECEIVER THAT HANDLES SCREEN ON AND SCREEN OFF LOGIC
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
BroadcastReceiver mReceiver = new ScreenReciever();
registerReceiver(mReceiver, filter);
}
3) Done