Getting Toast Message from Notification - android

Is it possible to have a Toast being shown when I click on one of the Variants of Notification? I tried to sending this Toast by intent to another class but could not find a way to do so.
Also, i have tried just creating intent to another class which just has a function of creating a Toast message upon being called, but this did not work because the Toast was being shown upon creation of Notification. Please help. Thanks.
Intent intent = new Intent();
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setTicker("Info")
.setContentTitle("Info ")
.setContentText("NotiText")
.addAction(R.drawable.ic_stat_name, "This should make Toast upon clicking", pendingIntentCall)
.setSmallIcon(R.drawable.ic_stat_name).setAutoCancel(true);

Your intent does nothing. When passing the Intent to PendingActivity.getActivity(...), you must define it as an explicit intent for an Activity. For example:
Intent intent = new Intent(this, SomeActivity.class);
But if you only want to show a toast message, it would be probably better not to start an activity, because that would be quite an expensive operation for that purpose. Instead, you can create a PendingIntent also for a service or a broadcast. For example:
Intent intent = new Intent(this, MyBroadcastReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
And MyBroadcastReceiver.java could look like this (basically):
public class MyBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context.getApplicationContext(), "Your message", Toast.LENGTH_SHORT).show();
}
}
Btw, it is recommended to add an action or extra to the intent when you create it, and check for that action or extra when receiving the intent to verify that it's actually the intent that you expected to receive.

Related

Working example of Recognizer PendingIntent flow

I have the following code setup to launch the voice recognizer with a pendingintent to launch another activity:
Intent voiceActivityIntent = new Intent (MainActivity.this, VoiceActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity (MainActivity.this, 0,
voiceActivityIntent, PendingIntent.FLAG_ONE_SHOT);
Intent intent = new Intent (RecognizerIntent.ACTION_RECOGNIZE_SPEECH)
.putExtra (RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM)
.putExtra (RecognizerIntent.EXTRA_RESULTS_PENDINGINTENT, pendingIntent);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
The documentation states that startActivity can't be used with RecognizerIntent.ACTION_RECOGNIZE_SPEECH, and using startActivityForResult simply returns the result to the current activity (MainActivity) which is not desired.
I've tried:
pendingIntent.send ();
but this simply take me to VoiceActivity.class without executing the recognizer.
I'm currently testing on the Android Wear Round API 21 emulator.
just add the following to your code:
// this intent wraps voice recognition intent
PendingIntent pendingIntVoice = PendingIntent.getActivity(context, 0, intent, 0);
pendingIntVoice.send();
In other words, with pendingIntent.send (); you're not calling the Voice Recognizer Intent, BUT the Intent that should be automatically called when voice recognition ends (infact you set it with putExtra (RecognizerIntent.EXTRA_RESULTS_PENDINGINTENT, pendingIntent) line)
I wanted something similar, here's what I came up with and it works. This is inside a Service:
//IntentHandlerService extends IntentService
//The intent I want sent back to me after voice recognition is complete...
Intent inputTextIntent = new Intent(this, IntentHandlerService.class);
//...gets wrapped in this PendingIntent...
PendingIntent pendingIntent = PendingIntent.getService(this, 0, inputTextIntent, PendingIntent.FLAG_ONE_SHOT);
//...and added to the intent aimed for the recognizer...
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(RecognizerIntent.EXTRA_RESULTS_PENDINGINTENT, pendingIntent);
//...which is the one you start.
startActivity(intent);

Android widget Intent without layout

is it possible to make an Intent whic runs in background? Like a service i assume.
I need to perform an action (call webservice) when the user clicks on a widget.
My code below
Intent intent = new Intent();
intent.putExtra("webServiceId", mWSId);
PendingIntent pending = PendingIntent.getActivity(context, 0,intent, 0);
views.setOnClickPendingIntent(R.id.btnWidgetToggle, pending);
appWidgetManager.updateAppWidget(mAppWidgetId, views);
Intent resultValue = new Intent();
resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
mAppWidgetId);
setResult(RESULT_OK, resultValue);
finish();
Should I make my intent to perform an action to a service or something like that?
Thanks!
UPDATE:
Im trying to do it by calling an IntentService now, but it doesnt calls it. My code below:
Intent intent = new Intent(context, widgetService.class);
intent.putExtra("webServiceId",mWSId);
PendingIntent pending = PendingIntent.getService(context, 0, intent, 0);
views.setOnClickPendingIntent(R.id.btnWidgetToggle, pending);
any idea?
I am not sure what you are trying to achieve
I am right you want to call a widget on click of a widget(view)
This can be achieved in a simple way by calling the onClick action
of a widget and place the web-service invocation code inside the
onCllickListener
Sample
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//Place your webservice invocation code here
}}
If you want to use service in some way have a look at android documentation on how to use it
Hope this helps !
I solved it by calling a IntentService.
Here is my code:
Intent intent = new Intent(context, widgetService.class);
intent.putExtra("webServiceId",mWSId);
PendingIntent pending = PendingIntent.getService(context, 0, intent, 0);
views.setOnClickPendingIntent(R.id.btnWidgetToggle, pending);
Thanks #Karakuri

onclicklistener for notification [duplicate]

This question already has answers here:
How to set click listener for notification?
(3 answers)
Closed 9 years ago.
I had set a notification in my app. Its working fine. If i click the notification in statusbar it takes to my app.
Now i need to set some work if the notification is clicked where can i set this?
Is there any method that implicitly got invoked when a notification is clicked?
Also i want to remove that notification if it is clicked,how to do so?
this is my code
notifManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Intent inty=getIntent();
note = new Notification(R.drawable.icon, "New E-mail", System.currentTimeMillis());
PendingIntent intent = PendingIntent.getActivity(MainActivity.this, 0, inty, 0);
note.setLatestEventInfo(MainActivity.this, "New E-mail", "You have one unread message.", intent);
notifManager.notify(R.string.search_hint, note);
You could add some extra data to the intent and then in your activity look for it in the onCreate and onNewIntent methods.
For example:
inty.putExtra("came from notification", true);
You can then read that out via the intent passed to onNewIntent or in onCreate by using getIntent().
intent.getBooleanExtra("came from notification", false);
Try to call broadcastReceiver it may helpful for your requirement,
Intent notificationIntent = new Intent(this, dummy_activity.class);
notificationIntent.setAction("android.intent.action.MAIN");
notificationIntent.addCategory("android.intent.category.LAUNCHER");
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
notificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT |
Notification.FLAG_AUTO_CANCEL);
// Now, once this dummy activity starts send a broad cast to your parent activity and finish the pending activity
//remember you need to register your broadcast action here to receive.
BroadcastReceiver call_method = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
String action_name = intent.getAction();
if (action_name.equals("call_method")) {
// call your method here and do what ever you want.
}
}
};
registerReceiver(call_method, new IntentFilter("call_method"));

Implement setOnClickPendingIntent from notification

I build custom notification that contain button and i want to listin when user press on it.
The button should not open any activity but only logic staff like change song.
the Code:
RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.notification);
contentView.setTextViewText(R.id.toptext, nowPlayingTitle);
//this not work
Intent intent = new Intent(this, receiver.class);
intent.putExtra("UNIQ", "1");
PendingIntent pendingIntent = PendingIntent.getBroadcast(
this.getApplicationContext(), 234324243, intent, PendingIntent.FLAG_CANCEL_CURRENT)
contentView.setOnClickPendingIntent(R.id.imageButtonPlay,
pendingIntent);
notification.contentView = contentView;
// this is to return to my activity if click somwhere else in notification
Intent notificationIntent = new Intent(this, MYACTIVITY.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.contentIntent = contentIntent;
mNotificationManager.notify(NOTIFICATION_ID, notification);
I don't get the hang of the setOnClickPendingIntent what need to be in the second param?
How can i just call a function after user press on the button?
im probably missing something cause i dont understand the receiver side and what happend after user press
You are missing the fact that the button you created actually doesn't belong to your application. It is created in another context, in another process. There is no way it can call your function.
Instead, when the user taps the button, that pending intent is fired. You can catch it by your receiver (in your activity), check some parameters and do the action.
Or you can implement a service and handle this intent in background. I'd prefer this way.
thanks for quick answer. I try using receiver but it never fired.
The code is in the main question and i created for the reciever class the following code:
public class receiver extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
Bundle bundle = intent.getExtras();
}
}
but click on the notification never fire the receiver ( Test on debug mode )

Window already focused, ignoring focus gain of: com.android.internal.view

Unfortunately, I was unable to find a resolution to this issue in other StackOverflow posts so I apologize in advance for re-posting this question. Essentially, I have an AppWidget that creates a Notification. I want the user to click the Notification to launch an Activity. The AppWidget and Activity are in the same APK.
The following code is all in the AppWidget. Clicking on the Notification broadcasts a custom Intent which is received by the AppWidget.
The problem is that the Activity does not appear to launch, even though logcat demonstrates that the custom Intent has indeed been broadcast. The only clue is the logcat message reiterated in the post title.
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Constants.BROWSER_INTENT)) {
launchActivity(context, R.layout.appwidget, R.id.launch_button, Activity.class);
}
super.onReceive(context, intent);
}
private void displayNotification(Context context, String ticker, String title, String msg) {
Intent intent = new Intent(context, ThisWidget.class);
intent.setAction(Constants.BROWSER_INTENT);
PendingIntent pending = PendingIntent.getActivity(context, 0, intent, 0);
NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.icon, ticker, System.currentTimeMillis());
notification.setLatestEventInfo(context, title, msg, pending);
notificationManager.notify(1, notification);
}
Some notes:
Pressing a button to execute launchActivity() works (it launches the
Activity)
launchActivity uses Intent.FLAG_ACTIVITY_NEW_TASK as
recommended by Android lifecycle documentation
Has anyone solved this issue when launching an Activity from a Notification press?
er, figures, I answered my own question just minutes after posting it. I guess I was over-complicating the implementation by using an Intent action. I changed these lines:
Intent intent = new Intent(context, ThisWidget.class);
intent.setAction(Constants.BROWSER_INTENT);
PendingIntent pending = PendingIntent.getActivity(context, 0, intent, 0);
to this:
Intent intent = new Intent(context, Activity.class);
PendingIntent pending = PendingIntent.getActivity(context, 0, intent, 0);
and it worked.
Sorry for the bogus post.

Categories

Resources