Intent Intents.Insert.ACTION only works the first time? - java

My Android service creates a notification with a custom button. The action for the custom button should open the "Create contact" system screen, with the phone number prefilled. This is the code:
// create the base notification
Intent notificationIntent = new Intent(context, MyService.class);
PendingIntent pendingIntent = PendingIntent.getService(
context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(title)
.setContentText(text)
.setAutoCancel(false)
.setOngoing(true)
.setContentIntent(pendingIntent);
// add the action to save to contacts
Intent saveIntent = new Intent(ContactsContract.Intents.Insert.ACTION);
saveIntent.setType(ContactsContract.RawContacts.CONTENT_TYPE);
saveIntent.putExtra(ContactsContract.Intents.Insert.PHONE, text);
saveIntent.putExtra(ContactsContract.Intents.Insert.PHONE_TYPE,
ContactsContract.CommonDataKinds.Phone.TYPE_WORK);
PendingIntent pendingSaveIntent = PendingIntent.getActivity(context, 0, saveIntent, 0);
builder.addAction(0, "Save to Contacts", pendingSaveIntent);
// show the notification
context.getSystemService(Context.NOTIFICATION_SERVICE).notify(0, builder.build());
The code works well the first time (let's say that I pass "01234-1234567" to it). However, subsequent calls keep showing the same dialog shown the first time, with the same value in it. So even if I call this piece code again with a different number in input (the value of "text" for the "Intents.Insert.PHONE" extra value) I'll always get "01234-1234567".
I tried cancelling the Contacts screen, manually create a new contact, completely stopping the Contacts apps...no matter what, next time I tap on the notification's button I'll get the screen with the value I originally passed on the first attempt.
Do you have any explanation or see anything wrong in the code?

I solved it. Basically, the system caches intents, unless they differ for something more than just the extra params. In my code I was only changing the phone number, so it was always using the same (first) cached intent. The solution is to use a flag to cancel the currently cached intent and create a new one
PendingIntent pendingSaveIntent = PendingIntent.getActivity(
context, 0, saveIntent, PendingIntent.FLAG_CANCEL_CURRENT);

The system cashes intents, so in your PendingIntent instead of typing 0 as flag, use (PendingIntent.FLAG_UPDATE_CURRENT), try this:
PendingIntent.getActivity(context, 0, saveIntent, PendingIntent.FLAG_UPDATE_CURRENT);

Related

How to set id for the views in RemoteViews (appwidget) programmatically?

I generate dates for the month calendar inside appwidget, using the code fragment below:
for (week in 0 until 6) {
val rowRv = RemoteViews(context.packageName, R.layout.w_calendar_row_week_wrapper)
for (day in 0..6) {
...
// Set date into cell
// I need to distinguish textviews later - to choose the date by the click
dateTextCellRv.setTextViewText(
android.R.id.text1,
cal.get(Calendar.DAY_OF_MONTH).toString()
)
// For click on the chosen date
val getDateNumber = cal.get(Calendar.DAY_OF_MONTH).toString().toInt()
val clickOnChosenDate: PendingIntent =
Intent(context, WidgetCalendarProvider::class.java).let { intent ->
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId)
intent.putExtra("passedDate$appWidgetId", getDateNumber)
intent.action = ACTION_CHOSEN_DATE_CLICK_CALENDAR
PendingIntent.getBroadcast(
context,
appWidgetId,
intent,
PendingIntent.FLAG_IMMUTABLE
)
}
dateTextCellRv.setOnClickPendingIntent(
android.R.id.text1,
clickOnChosenDate
)
rowRv.addView(R.id.row_week_wrapper_w_calendar, cellRv)
cal.add(Calendar.DAY_OF_MONTH, 1)
}
rViews.addView(R.id.calendar_view_w_calendar, rowRv)
}
How to set id for all the textviews with dates programmatically?
Is it possible to distinguish textviews with android.R.id.text1 for getting string values from each of them by click on any TextView?

How can i direct to the PDF file generated by clickable notification

Hello, I am making a project where the record contains ID, Name, Status And I have save it as a PDF the data, and its gets a notification after getting saved,
No I wanted to know how can I make that clickable and direct to file.
Below is the code I tried, but not working
Intent notificationIntent = new Intent(Intent.ACTION_VIEW);
notificationIntent.addCategory(Intent. CATEGORY_APP_FILES) ;
notificationIntent.setDataAndType(Uri.parse(filePath), "application/pdf");
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
PendingIntent resultIntent = PendingIntent. getActivity (SheetActivity. this, 0 , notificationIntent , 0) ;
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder
(SheetActivity. this, default_notification_channel_id )
.setSmallIcon(R.drawable. mcclogo )
.setContentTitle("PDF saved")
.setContentText(filePath)
.setContentIntent(resultIntent) ;

retrieve data from activity

My app takes data from 4 EditTexts, puts them in an ArrayList and with a press of a button (using Intents) passes the data to second activity which then prints that data in 4 TextViews and has a button that returns me to first activity for next input. Is there any way of keeping all of my input data so I can show everything I've inputted in a TextView or Toast message?
Let's assume your first Activity is A and second activity is B
so in the A activity's button press you can call the 2nd activity using following code.
Intent intent = new Intent(A.this, B.class);
String input1 = txt1.getText().toString();
String input2 = txt2.getText().toString();
intent.putExtra("key1", input1 );
intent.putExtra("key2", input2 );
startActivity(intent);
From the B activity you can access these values using following code
Intent intent = getIntent();
String input1 = intent.getStringExtra("key1");
String input2 = intent.getStringExtra("key2");
yes you can. you can pass your data to second activity like this.
Intent intent1= new Intent(first.this, second.class);
Bundle bundle1=new Bundle();
bundle1.putString("data", et1.getText().toString());
intent1.putExtras(bundle1);
startActivityForResult(intent1, 0);
or you can restore them in a temporary database.

How to pass selected data over to next activity android

Im creating a simple app which is for example i got 10 table row with check box and user click on 3 box and press next, i wan to print out only the 3 row the use clicked and adding extra button/icon beside it. Is it possible? please help ! Thank you !
to pass data to another Activity you can use Intents and putting your data as extra :
here is a sample :
Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra("data", "My data in second activity");
startActivity(intent);
and for retrieving data in SecondActivity :
String data = getIntent().getStringExtra("data");
Yes, use putExtra and pass an ArrayList of String:
Intent intent = new Intent(Recipes2.this, XMLParser.class);
intent.putStringArrayListExtra("myList", (ArrayList<String>) myList);
startActivity(intent);
Here is the answer:
Intent intent = new Intent(getBaseContext(), SignoutActivity.class);
intent.putExtra("EXTRA_SESSION_ID", sessionId);
startActivity(intent)
For more information check this link.

Select multiple images from Photo Gallery on Android using Intents

#See this https://stackoverflow.com/a/15029515/185022
I`m trying to select images from gallery, but i only found the way to select a single image.
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, 1);
Is there a way to select multiple images?
Create a custom gallery same like: Android custom image gallery with checkbox in grid to select multiple
First of all you need to use putExtra with your photoPickerIntent
photoPickerIntent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE);
Then in your on activity result you should get ClipData from Intent like this
ClipData clipData = data.getClipData();
//Where data is param intent of onActivityForResult
And iterate this clipData to get URI for specific picked image.
for (int i = 0; i < clipData.getItemCount(); i++){
Uri uri = clipData.getItemAt(i).getUri();
}
I hope this helps
Why don't you try ACTION_SEND_MULTIPLE thing. You will receive a set of Uris.
Something like
if (Intent.ACTION_SEND_MULTIPLE.equals(action))
&& Intent.hasExtra(Intent.EXTRA_STREAM)) {
ArrayList<Parcelable> list =
intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM);
for (Parcelable parcel : list) {
Uri uri = (Uri) parcel;
/// do things here.
}
}
Saw this code block on a google-groups post. Just try this out.
Thanks.
I think, you should implement custom gallery for multiple image pick action.
see here in details.

Categories