I want to open a calendar (for instance, google calendar) from my widget app. I did something like that:
Intent intent2 = new Intent();
//Froyo or greater
ComponentName cn = new ComponentName("com.google.android.calendar", "com.android.calendar.LaunchActivity");
intent2.setComponent(cn);
PendingIntent pendingIntent2 = PendingIntent.getActivity(context, 1,
intent2, 0);
updateViews.setOnClickPendingIntent(R.id.calendar, pendingIntent2);
But it doesn´t work. Any help?
Thank you very much
Finally it works:
Intent intent2 = new Intent();
ComponentName cn = new ComponentName("com.android.calendar",
"com.android.calendar.LaunchActivity");
intent2.setComponent(cn);
PendingIntent pendingIntent2 = PendingIntent.getActivity(context, 1,
intent2, 0);
updateViews.setOnClickPendingIntent(R.id.calendar, pendingIntent2);
and in the manifest:
<activity
android:name="com.android.calendar.LaunchActivity">
</activity>
Related
I want to run my application after notification click!
I want to run: app-->com.zaglebiefm.Radio
This file diectory: library-->com.zaglebiefm.library.Notyfication
Notification.class:
private void buildNotification() {
Intent intentPlayPause = new Intent(NOTIFICATION_INTENT_PLAY_PAUSE);
Intent intentOpenPlayer = new Intent(NOTIFICATION_INTENT_OPEN_PLAYER);
Intent intentCancel = new Intent(NOTIFICATION_INTENT_CANCEL);
PendingIntent playPausePending = PendingIntent.getBroadcast(this, 23, intentPlayPause, 0);
PendingIntent openPending = PendingIntent.getBroadcast(this, 31, intentOpenPlayer, 0);
PendingIntent cancelPending = PendingIntent.getBroadcast(this, 12, intentCancel, 0);
RemoteViews mNotificationTemplate = new RemoteViews(this.getPackageName(), R.layout.notification);
Notification.Builder notificationBuilder = new Notification.Builder(this);
if (artImage == null)
artImage = BitmapFactory.decodeResource(getResources(), R.drawable.default_art);
mNotificationTemplate.setTextViewText(R.id.notification_line_one, singerName);
mNotificationTemplate.setTextViewText(R.id.notification_line_two, songName);
mNotificationTemplate.setImageViewResource(R.id.notification_play, isPlaying() ? R.drawable.btn_playback_pause : R.drawable.btn_playback_play);
mNotificationTemplate.setImageViewBitmap(R.id.notification_image, artImage);
mNotificationTemplate.setOnClickPendingIntent(R.id.notification_play, playPausePending);
Notification notification = notificationBuilder
.setSmallIcon(smallImage)
.setContentIntent(openPending)
.setPriority(Notification.PRIORITY_DEFAULT)
.setContent(mNotificationTemplate)
.setUsesChronometer(true)
.build();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
RemoteViews mExpandedView = new RemoteViews(this.getPackageName(), R.layout.notification_expanded);
mExpandedView.setTextViewText(R.id.notification_line_one, singerName);
mExpandedView.setTextViewText(R.id.notification_line_two, songName);
mExpandedView.setImageViewResource(R.id.notification_expanded_play, isPlaying() ? R.drawable.btn_playback_pause : R.drawable.btn_playback_play);
mExpandedView.setImageViewBitmap(R.id.notification_image, artImage);
mExpandedView.setOnClickPendingIntent(R.id.notification_expanded_play, playPausePending);
notification.bigContentView = mExpandedView;
}
if (mNotificationManager != null)
mNotificationManager.notify(NOTIFICATION_ID, notification);
}
I tried some things but they won't work for me.
Any ideas or solutions.
Use the below code instead to launch desired/targetted application activity.
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, new Intent(context,YourTargetActivity.class), 0);
mBuilder.setContentIntent(pendingIntent);
This should work.
Intent myintent = new Intent(this, Splash_Activity.class);
myintent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_NEW_TASK);
int randomPIN = (int)(Math.random()*9000)+1000;
PendingIntent conIntent = PendingIntent.getActivity(this, randomPIN,
myintent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(conIntent);
This will work
I have this code to make the actions:
Intent playIntent = new Intent(Intent.ACTION_VIEW);
playIntent.setDataAndType(uri, path.contains(".jpg") ? "image/jpeg" : "video/mp4");
PendingIntent play = PendingIntent.getActivity(context, 1, playIntent, 0);
mBuilder.addAction(R.mipmap.ic_play_arrow_black_48dp, "", play);
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
shareIntent.setType(path.contains(".jpg") ? "image/jpeg" : "video/mp4");
PendingIntent share = PendingIntent.getActivity(context, 2, shareIntent, 0);
mBuilder.addAction(R.mipmap.ic_share_white_48dp, "", share);
Intent doneIntent = new Intent(context, NotificationCloser.class);
doneIntent.putExtra("notificationId", notificationId);
PendingIntent done = PendingIntent.getBroadcast(context, 3, doneIntent, 0);
mBuilder.addAction(R.mipmap.ic_done_black_48dp, "", done);
And this is my broadcast receiver
public class NotificationCloser extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
int id = intent.getIntExtra("notificationId", 0);
Log.i("MyInfo", "NotificationCloser.onReceive(" + id + ")");
MainActivity.mNotifyManager.cancel(id);
}
}
When I click in play or share button, it does according the function, opens the default app to view images or videos but doesn't close the notification. When I click in the done button, ONLY in the first time i receive the id correctly, after the first time it gets the id sent in the first time
Can someone help me?
I solved the problem of the broadcast by using random request codes, also added the flag PendingIntent.FLAG_UPDATE_CURRENT. I used in all because sometimes the share bugged and used another image.
PendingIntent play = PendingIntent.getActivity(context, new Random().nextInt(150) * 37, playIntent, 0);
PendingIntent share = PendingIntent.getActivity(context, new Random().nextInt(150) * 37, shareIntent, 0);
PendingIntent done = PendingIntent.getBroadcast(context, new Random().nextInt(150) * 37, doneIntent, PendingIntent.FLAG_UPDATE_CURRENT);
This answer was posted as an edit to the question Notification with Multiple actions by the OP Rodrigo Butzke under CC BY-SA 3.0.
I have two alarms and a broadcastreceiver:
AlarmManager am2=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent2 = new Intent(context, MyReceiverAlarm.class);
intent2.putExtra("name", "juan");
PendingIntent pi2 = PendingIntent.getBroadcast(context, 0, intent2, 0);
am2.set(AlarmManager.RTC_WAKEUP,System.currentTimeMillis()+1000, pi2);
AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, MyReceiverAlarm.class);
intent.putExtra("name", "jose");
PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);
am.set(AlarmManager.RTC_WAKEUP,System.currentTimeMillis()+8000, pi);
However, I'm always receiving the first one, only. I always get "juan" and the alarm is fires after 8 seconds.
I don't understand the reason...If i have two alarms why fires one ?
My BroadcastReceiver:
Toast.makeText(context, extras.getString("nombre") + "", Toast.LENGTH_LONG).show();
String name=extras.getString("nombre");
if (extras != null && name.equals("juan") {
String callForwardString = PHONE ;
Intent intentCallForward = new Intent(Intent.ACTION_CALL);
Uri uri2 = Uri.fromParts("tel", callForwardString, "#");
intentCallForward.setData(uri2);
intentCallForward.addFlags(intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intentCallForward);
} else {
...
}
Is not possible to program two alarms?
Thanks in advance
Try to change the request code (second parameter passed to getBroadcast():
PendingIntent pi2 = PendingIntent.getBroadcast(context, 2, intent2, 0);
PendingIntent pi = PendingIntent.getBroadcast(context, 1, intent, 0);
How to pass thread id through pending intent to open exact message conversation, like when we get a message with click notification and see the conversation
PendingIntent nPendingInten = PendingIntent.getActivity(context, 0, nIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
Intent intent = new Intent(context, ConvActivity.class);
intent.putExtra(ConvActivity.THREAD_ID_EXTRA, threadId);
intent.putExtra(ConvActivity.THREAD_NAME_EXTRA,listItem.getContactName());
intent.putExtra(ConvActivity.THREAD_IMAGE_EXTRA, uri);
intent.putExtra(ConvActivity.THREAD_NUM_EXTRA,listItem.getAddress());
You are passing another intent to your pending intent - pay attention.
Intent intent = new Intent(context, ConvActivity.class);
intent.putExtra(ConvActivity.THREAD_ID_EXTRA, threadId);
intent.putExtra(ConvActivity.THREAD_NAME_EXTRA,listItem.getContactName());
intent.putExtra(ConvActivity.THREAD_IMAGE_EXTRA, uri);
intent.putExtra(ConvActivity.THREAD_NUM_EXTRA,listItem.getAddress());
PendingIntent nPendingInten = PendingIntent.getActivity(context, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
try this code
PendingIntent nPendingInten = PendingIntent.getActivity(context, 0, nIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
Intent intent = new Intent(context, ConvActivity.class);
intent.putExtra(ConvActivity.THREAD_ID_EXTRA,
threadId.longvalue);// change here
intent.putExtra(ConvActivity.THREAD_NAME_EXTRA,listItem.getContactName());
intent.putExtra(ConvActivity.THREAD_IMAGE_EXTRA, uri);
intent.putExtra(ConvActivity.THREAD_NUM_EXTRA,listItem.getAddress());
I need to display notification with two buttons. Different operation need to perform for each button.so for that I have written following code but when I'm getting multiple notification delete action is not performing.
Random NOTIFICATION_ID = new Random();
int CANCELNOTIFICATIONID = NOTIFICATION_ID.nextInt();
// define sound URI, the sound to be played when there's a notification
Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Log.i("******* Service6", "" + msg);
// intent triggered, you can add other intent for other actions
Intent intent = new Intent(GcmIntentService.this, LoginActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(GcmIntentService.this, 0, intent, 0);
Intent deleteIntent = new Intent(GcmIntentService.this, DeleteArchiveLoopActivity.class);
deleteIntent.putExtra(LoopMeConstants.EXTRA_DELETE_ARCHIVE_LOOPS, "Delete loops");
Trace.i(TAG, "Looptype Delete loop");
deleteIntent.putExtra("DELETE_ARCHIVE_LOOP_ID", loopId);
deleteIntent.putExtra("NOTIFICATONID", CANCELNOTIFICATIONID);
deleteIntent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
// PendingIntent pDeleteIntent = PendingIntent.getActivity(this, 145623, deleteIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Intent archiveIntent = new Intent(GcmIntentService.this, DeleteArchiveLoopActivity.class);
Trace.i(TAG, "Looptype Archive loop");
archiveIntent.putExtra(LoopMeConstants.EXTRA_DELETE_ARCHIVE_LOOPS, "Archive loops");
archiveIntent.putExtra("DELETE_ARCHIVE_LOOP_ID", loopId);
archiveIntent.putExtra("NOTIFICATONID", CANCELNOTIFICATIONID);
archiveIntent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
// PendingIntent pArchiveIntent = PendingIntent.getActivity(this, 145623, archiveIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
notificationBuilder.setSmallIcon(R.drawable.ic_launcher);
notificationBuilder.setContentTitle("Sample");
notificationBuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(mPushtext));
notificationBuilder.setContentText(mPushtext);
notificationBuilder.setSound(soundUri);
notificationBuilder.addAction(R.drawable.delete, "Delete", PendingIntent.getActivity(this, 145623, deleteIntent, PendingIntent.FLAG_ONE_SHOT));
notificationBuilder.addAction(R.drawable.archive, "Archive", PendingIntent.getActivity(this, 145623, archiveIntent, PendingIntent.FLAG_UPDATE_CURRENT));
notificationBuilder.setAutoCancel(false);
// notificationBuilder.setContentIntent(pArchiveIntent);
Log.i("GCMIntent Srevice5", "" + msg);
notificationBuilder.setOngoing(true);
mNotificationManager.notify(CANCELNOTIFICATIONID, notificationBuilder.build());
How to solve this issue.
You need to use different requestCode in Pending intent
notificationBuilder.addAction(R.drawable.delete, "Delete", PendingIntent.getActivity(this, 1, deleteIntent, PendingIntent.FLAG_ONE_SHOT));
notificationBuilder.addAction(R.drawable.archive, "Archive", PendingIntent.getActivity(this, 2, archiveIntent, PendingIntent.FLAG_UPDATE_CURRENT));
In the below way i have solved it.
in the intent i have given two differentactivities
Intent deleteIntent = new Intent(GcmIntentService.this, DeleteLoopActivity.class);
Intent archiveIntent = new Intent(GcmIntentService.this, ArchiveLoopActivity.class);
notificationBuilder.addAction(R.drawable.delete, "Delete", PendingIntent.getActivity(this, CANCELNOTIFICATIONID, deleteIntent, 0));
notificationBuilder.addAction(R.drawable.archive, "Archive", PendingIntent.getActivity(this, CANCELNOTIFICATIONID, archiveIntent, 0));
The above code solves my problem
Thanks all