How to check who called onUpdate - android

i have a problem with android widget. All i have is a imagebutton in the layout. if the user presses the button some operation (changing a boolean value) in background via a service is done and it works perfectly. now i need an alarmmanager to check every 5 minutes the status of the boolean value (maybe another user has changed it. This also works but i can not differ if the service was called by button (to change the value) or by alarmmanager (to check the value).
Are there any flags i can set, e.g. to the alarmmanager? Does anyone hav any ideas how to do this?

I guess you could use intent extras in the intent that you send when button has been pressed, to indicate that it was from button press.

Related

Service referenced by local variable, yet holds value between function calls

I was playing around with services and dialogs, and I got a doubt. Within a dialog, I am starting a service like this:
Intent lock = new Intent(getActivity(),AppLockService.class);
getActivity().stopService(lock);
getActivity().startService(lock);
Now the first time I call the dialog through
dialog_name.show(getFragmentManager(), "dropbox");
Upon pressing the OK button, the intent is launched. Now later, during the same app execution, the dialog is triggered again ( which is according to my code logic -- nothing wrong here). The code in the dialog then stops the previously triggered intent and starts the new intent.
My question is this:
lock is a local intent variable as per my definition. So how does it know that it has to stop that particular service I have triggered here the first time? Would someone please explain this to me?
You don't have to keep track of the service in a variable because Android does it for you.
The way that the OS treats a service is that it will not allow more than one instance of the service be to running at any time.
So at any moment there are 0 or 1 instances of your service. If there are 0, no problem, the OS will ignore the call to StopService. If there is 1 instance, it must be the instance you started previously - so it will be stop that one.

Android: Make app transparent and act on the background application

Is it feasible to make our Android application completely transparent (as if its not active at all) and work on the other apps?
My Requirement:
First last an app and after some few settings, make it transparent. Once its transparent, the user will not know that this app is active, however, our app should respond to only specific controls.
This is because of the Broadcast receiver limitation, I will have to use the Volume button for some actions in my application. But, this button doesn't broadcast. So, currently I am using Power button which is not the requirement.
Please throw some light on this. I did some research but, couldnt find any. :(
This is because of the Broadcast receiver limitation, I will have to use the Volume button for some actions in my application. But, this button doesn't broadcast.
I am not sure it this is right. If you read Android BroadCastReceiver for volume key up and down question, it seems that you can detect it in BroadCastRceiver. I've never tried but it might be worth a try. Do something like following:
In your BroadcastReceiver onReceive function, do something like following:
public void onReceive(Context arg0, Intent intent) {
if (intent!=null){
int volume = (Integer)intent.getExtras().get("android.media.EXTRA_VOLUME_STREAM_VALUE");
// Get the old volume from the SharedPOreferences
// volume variable contains the current volume
// Compare it to the old value saved. If it is greater than old value then user pressed the UP Volume button else DOWN volume.
}
}
Also I am not sure that you can make it transparent and still keep it running. You can have a background of an activity as transparent though. How do I create a transparent Activity on Android?. Hope it helps.

Cancelling Alarms with PendingIntents

I am trying to cancel an alarm that was set last time my app was run. This alarm has a PendingIntent that was set with PendingIntent.getBroadcast and an inner Intent that contains some variables set by intent.putExtra. My question is this, I know that alarms can be canceled by calling alarmManager.cancel(pendingIntent) where pendingIntent is the same as the one used to set the alarm. But, if the variables placed into the intent are changed will the alarm still be canceled? For example, I set an alarm with intent.putExtra("Joe") where Joe is a contact name. Later my app is closed and when it is re-run I try and cancel the alarm for "Joe" but the user has changed the name of the contact to "Jones". Can I cancel the alarm without knowing the variables I put into the intent?
Thanks!
I think it should cancel the alaram anyway, even though some data is different. The cancel method says:
Any alarm, of any type, whose Intent matches this one (as defined by filterEquals(Intent)), will be canceled.
And filterEquals says:
Determine if two intents are the same for the purposes of intent resolution (filtering). That is, if their action, data, type, class, and categories are the same. This does not compare any extra data included in the intents.
Anyhow, I'd still test it myself.
According to this question (which references the documentation), anything you add using putExtra is not taken into account when checking if an intent is equal to another one.
It shouldn't matter if the extra data is changed.

PendingIntents won't fire after attempting to add 2nd Home screen widget

I have a widget that sets up a number of pendingIntents. All is working just fine on the widget as long as I only have a single widget on the home screen.
For the time being I decided that I was going to limit the user to only one of my home screen widgets. If one is already in use the ConfigureActivity gives the user an alertDialog box telling them they can't add a second one and then sets setResult(RESULT_CANCELED, null) and then finish().
This works just fine and keeps the user from being able to setup any further home screen widgets but for the one that is already in place most of my pendingIntents won't fire. Logcat tells me "Cannot send pending intent:"
Each of the pendingIntents is set to start up a service that is used to update the widget. If I remove the widget and reset it up it works just fine again, until I try to add a second widget. It is a bit baffling to me at this point. Anyone have any ideas as to why starting to add the 2nd widget but not completing it would cancel out my previous pendingIntents?
Incase it matters, the pendingIntents where setup with FLAG_CANCEL_CURRENT.
Here is the code used to set my intent and pendingIntents.
Intent intentDialog = new Intent(getBaseContext(), ScheduleActionsActivity.class);
intentDialog.putExtra("Action", ACTION_ENTER_SCHEDULE);
intentDialog.setAction("abc.hwRowOne");
intentDialog.putExtra("scheduleId", sch.getId());
intentDialog.putExtra("scheduleDescription", sch.getDescription());
PendingIntent pendingIntentDialog1 = PendingIntent.getActivity(getBaseContext(), 0, intentDialog, PendingIntent.FLAG_CANCEL_CURRENT);
views.setOnClickPendingIntent(R.id.hwRowOne, pendingIntentDialog1);
The line where intentDialog.setAction() changes to be unique for each row of the widget, so I thought that was making my pendingIntent unique as well.
I saw this answer last night after posting my question but I am not sure why/how it works and I am unsure where to put it in my area. It appears to set a unique data for each intent, but that is what I thought my .setAction() was doing. Multiple Instances Of Widget Only Updating Last widget
So to answer your question, no I am not using the appWidgetId at all in my intents, do I just need to pass it along as a putExtra() then on the intentDialog?
The answer to this question solved my issues:
Multiple Instances Of Widget Only Updating Last widget
I have no idea why adding the setData(uri) works but it did allow me to have more than one widget on the home screen now and the pendingIntents are unique and firing as I would expect them.
On the safe side I am also adding the appWidgetId to the intent and might need it later if I decide to allow more than one widget per device.

Status bar Notification in android

I want to set a onClickListner for a status bar Notification. How it is possible ? Please help. Now i can load a Activity by using the pending intent. I like to set a onClickListner for the notificatioin.
Regards
Parvathi
It is not possible to set an OnClickListener for a notification. Because of the way notifications are handled/displayed there is no way to guarantee that the process that created the notification will be running at the time the notification is clicked. This means that any code you wrote to provide click handling may not be running.
If you need click listener style behavior you will have to do it using the PendingIntent: set it to start a Service that runs the logic or to use an Intent that is received by a BroadcastReceiver. This will let you perform activity without requiring a UI.

Categories

Resources