In this code snippet, what Activity class do I use? The one where the code is written? Is this statusbar message going somewhere?
Intent notificationIntent = new Intent(this, MyActivity.class);
Does this method act immediately or it's done later?
NotificationManager.notify();
In this code snippet, what Activity class do I use?
How should we know? We are not mind readers, and you have given us one line out of a much larger example somewhere.
Taking a completely random guess, that is probably the activity that should be opened when the user taps on the notification entry.
Does this method act immediately or it's done later?
The call is asynchronous, but it should be displayed almost immediately.
Related
I currently have an intent service that is managing my geofence transitions and I am hoping to use the geofence ID to trigger a sequence of events either by starting an activity through another intent or by broadcasting the relevant string to my main activity so that I can do the process there. currently my code inside my intentservice contains this
String fenceID = fences.get(0).getRequestId();
Log.i( fenceID, "fenceID is");
if (fenceID == Home){
Intent broadcastIntent = new Intent();
broadcastIntent.setAction(PlayerActivity.MyWebRequestReceiver.PROCESS_RESPONSE);
broadcastIntent.addCategory(Intent.CATEGORY_DEFAULT);
broadcastIntent.putExtra(fenceID, fenceID);
broadcastIntent.putExtra(RESPONSE_MESSAGE, "respone");
sendBroadcast(broadcastIntent);
/* Intent mpdIntent = new Intent(this, PlayerActivity.class)
.setData(Uri.parse("http://maxmarshall.ddns.net/segments/TEST_dash.mpd"))
.putExtra(PlayerActivity.CONTENT_ID_EXTRA, "my test")
.putExtra(PlayerActivity.CONTENT_TYPE_EXTRA, 0);
startActivity(mpdIntent);*/
}
You can see the commented out code is my original idea or end goal (whether it is completed through this set up or in another activity
However even though fenceID is Home I am not getting any response to that effect with this setup. Can anyone shed some light on what I am doing wrong here? I can update the question with more code if anyone needs. perhaps I am not registering my broadcast receiver properly if theres something I need to put in the manifest? anyway thanks very much for any and all help!
You cannot start a new Activity from a Service context in the same task. The only way you can start an Activity from there is to use the flag FLAG_ACTIVITY_NEW_TASK. Make sure you really need it before you implement this.
Your code for sending a broadcast message looks fine. Just make sure you register your receiver properly and it's IntentFilter matches.
Also, never do this:
String fenceID = fences.get(0).getRequestId();
Log.i( fenceID, "fenceID is");
if (fenceID == Home){
The fenceId is a String, i.e. it is a an Object. Comparing with == will give you true only if both references point to the same object. Is this really what you want in this case? Use fenceId.equals(Home) if you want to compare characters instead of objects.
My guess is that your code is never executed because of this mistake in the condition.
Is there a way to tell if I reached some Activity through regular flow of the app or whether I reached this (deep) Activity from a Notification via PendingIntent?
I need to perform some operations when the application starts and if I got to this Activity via notification I need to make sure these operations are made.
You can put an extra on your PendingIntent and than when the activity starts check for it using getIntent() ("Return the intent that started this activity."). Doing so you can getExtras() and check how the activity was started.
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.
I've been searching for this for a while and keep coming up short. However I have setup notifications before where selecting it launches an activity, now I'm trying to mod that code.
This time I'm looking for a solution to run just a single line of code when the notification is selected. I want it to set a button from invisible to visible. So when my notification is cleared I want the code below to take place, not launch an activity or intent:
butNX.setVisibility(0);
Any input on the matter? thanks in advance.
I wasn't try this, but i think it will resolve your problem.
1. Set up a Receiver where you need it.
2. Create an Intent and add the extras to it something like this: intent.putExtra("MODE","VIS");.
3. Craete a PendingIntent which will broadcast your intent to your receiver.
PendingIntent pIntent=PendingIntent.getBroadcast(yourContext,yourReqCode,
yourIntent,yourFlags);
4.Set notification
So while receiver will receive your broadcast it should to check what to do with this intent by data which you send...
Ok,
I am working on an Android app that uses a service to collect & process GPS data. The service also creates a notification in the status bar. What I cannot figure out is how to have the notification open the main class when a user clicks on it. From what I've found online it shouldn't be that hard to do, but I cannot get it to work. Does anyone have some suggestions on where to look?
Intent notificationIntent = new Intent(this, MyClass.class);
This line of code is from http://developer.android.com/guide/topics/ui/notifiers/notifications.html.
Simply replace MyClass with your activity class - it will be launched when you click the notification.