How can I add a contact programmatically? - android

I use the following code to add a contact :
Intent intent = new Intent(Intent.ACTION_INSERT, ContactsContract.Contacts.CONTENT_URI);
activity.startActivityForResult(intent, ATTACH_VCARD_REQUEST_CODE);
But on my Google Nexus the default Android Contact Manager application doesn't return result to my activity
I have the following behaviour:
1. I call contact manager
2. Add field name and click "Done"
After that opens other Activity of default Android Contact Manager, and I can leave this activity only if I press back or up button. After that I can't return to my application activity using back stack (back button). I can open my application with Recent Tasks Manager, but then I get resulCode == 0 and intent == null.

I reviewed source code of standart Contacts application and found Activity which is responsible for creating new contact. There is flag INTENT_KEY_FINISH_ACTIVITY_ON_SAVE_COMPLETED = "finishActivityOnSaveCompleted"; If this flag is true then called setResult(RESULT_OK);
So solution of this problem is
Intent intent = new Intent(ContactsContract.Intents.Insert.ACTION);
intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
intent.putExtra("finishActivityOnSaveCompleted", true);
activity.startActivityForResult(intent, ATTACH_VCARD_REQUEST_CODE);

Related

Dynamic Shortcuts to open Fragments in Android

Hi I'm developing an application that has 2 types of user: Admin and normal user; The admin obviously has more actions than the normal user, so I need to use dynamic Shortcuts, depending on the user the Shortcuts are created.
At this point everything is already codified and working. However, at the time of assigning an Intent, even to the MainActivity I receive an error.
Intent intent = new Intent();
intent.setComponent(new ComponentName("com.motusk.Monit", "com.motusk.Monit.activity.MainActivity"));
shortcut.add(new ShortcutInfo.Builder(this, "ub")
.setShortLabel("Location")
.setLongLabel("Location")
.setIcon(Icon.createWithResource(this, R.drawable.bg))
.setIntent(intent)
.build());
I also tried with:
Intent intent = new Intent(this, MainActivity.class);
But, in both I get the error:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.motusk.Monit/com.motusk.Monit.activity.MainActivity}: java.lang.NullPointerException: intent's action must be set
In what I need help with is:
1) How to create a correct Intent to the MainActivity?
2) How to know which shortcut was pressed? (The process of depending on which shortcut was pressed to open a certain Fragment will be performed in the MainActivity, that's why I need to know which Shortcut was pressed).
As per the error message, your Intent must set an action with setAction:
intent.setAction("LOCATION_SHORTCUT");
You can then check the action in your Activity:
String action = getIntent() != null ? getIntent().getAction() : null;
if ("LOCATION_SHORTCUT".equals(action)) {
// Show the correct fragment
}

Application button to open current/new instance of another app

I have two applications, A and B. In app A I've added a button which opens app B.
When the button is clicked I want to open App B if it's not already running, otherwise I want to bring the app to the front.
This is the code I use:
Intent intent = getPackageManager()
.getLaunchIntentForPackage(
"com.myapp.something");
if (intent != null) {
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
The code seems to work, but the problem is that if I open app B from app A (using this code) and then click directly the icon of App B, I get two instances of the app, which is undesired.
How can I open the app, as the code does, but also get the same instance even if the app's icon is clicked?
Add Intent.FLAG_ACTIVITY_CLEAR_TOP

App Restarts on bringing it to foreground from different launch sources

Hi I am stuck with this issue.
I have my app which has 3 activities:
SplashScreenActivity, LoginScreenActivity, ViewPagerActivity(which houses 3 fragments).
When I put the apk in the mobile sdcard and install and open using the packagemanager. My App starts up just fine.
Issue - But, now if I press the Home Button and again launch the app from the Apps drawer/Homescreen. The App seems to relaunch and I have to go through the entire flow of Splash and LoginScreen.
This issue does not occur if I launch the App the first time itself from the Apps drawer itself./If I long press the Home Button and select the App from recent apps list the app is resumed properly as well.
For Reference I launch activities using these flags
Splash->Login
Intent intent=new Intent(SplashScreen.this, LoginActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
LoginActivity->ViewPagerActivity
Intent intent = new Intent(context, ViewPagerActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
Home screen icon launches whatever activity you have declared as the MAIN ... LAUNCHER activity in your manifest. Generally, the launch activity in manifest should be the main activity of your app. From there you can invoke splash screens and login activities when needed.
remove these flags or the complete line of code
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
This seems to be an issue when launching with package manager.
https://code.google.com/p/android/issues/detail?id=2373
if (!isTaskRoot()) {
Intent intent = getIntent();
String action = intent.getAction();
if (intent.hasCategory(Intent.CATEGORY_LAUNCHER) && action != null && action.equals(Intent.ACTION_MAIN)) {
finish();
return;
}
}

Prompt for default activity without actually opening activity

I need to set the default app for a specific mime type. I know how to clear the default but I need to then prompt the user without actually opening the app.
PackageManager p = mContext.getPackageManager();
ComponentName cN = new ComponentName(mContext, FakeDownloadActivity.class);
p.setComponentEnabledSetting(cN, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
Intent selector = new Intent(Intent.ACTION_DEFAULT);
selector.addCategory(Intent.CATEGORY_DEFAULT);
selector.setType(mimeType);
mContext.startActivity(selector);
p.setComponentEnabledSetting(cN, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
The code above launches the activity rather than ONLY selecting the default activity. It works be enabling a fake activity then disabling it. This causes the Select Default App dialog to show the next time it is called. I simply want to ONLY select the default activity.
What you are looking for is an ACTION_PICK_ACTIVITY intent.
First, you create an intent that defines the apps that should be eligible to choose, for instance:
Intent mainIntent = new Intent(Intent.ACTION_DEFAULT, null);
mainIntent.addCategory(Intent.CATEGORY_DEFAULT);
Then, you create the ACTION_PICK_ACTIVITY intent, and as an Extra, you pass the main intent you created before
Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Now, you just start an activity for result with this intent:
startActivityForResult(pickIntent, 0);
And a dialog will be created where the used can pick an application, but when clicked, the activity is not launched, instead, it will stay in your activity, and the function onActivityResult will be called with the results. So you need to create that function:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//In data, you have all the information about the selected application
if (data != null) {
//You can launch the application that we just picked with startActivity(data);
//or explore the variable to get all the information than you want
}
}
Take a look at the Intent class. There you have information about the package name, and the class that would be launched.
From now, what you need is to set that package and class as the default to the intent, or whatever else you need. The bad side, is that you only can save that information for your own internal purposes, for example to decide what app to launch next time that the users performs some action. What you cannot do is to modify the system settings to set a default activity for a given intent. Actually, the package manager has the addPreferredActivity method, that was supposed to do this, but it is deprecated since API level 8, giving this reasons:
This is a protected API that should not have been available to third
party applications. It is the platform's responsibility for assigning
preferred activities and this cannot be directly modified. Add a new
preferred activity mapping to the system. This will be used to
automatically select the given activity component when
Context.startActivity() finds multiple matching activities and also
matches the given filter.

How to call contact picker from home screen?

I'm experimenting with creating a custom Android Home Screen. I've used the sample Home Screen application and have adapted it. Something I would like to do, is open the contact picker from a button in the Home Screen and use the contact that the user chose in a next action. I've stumbled on the same problem that is mentioned in this question.
How can I work around this so that the home screen stays "singleInstance" and I can also call startActivityForResult()?
Is the contacts picker an activity that I can subclass (I've searched but can't find any) so that I can use the solution that David Wasser proposes in the aforementioned question?
I've found an elegant solution after all:
My main activity launches an intermediate, invisible activity that has android:theme="#android:style/Theme.NoDisplay"
This intermediate activity calls the contact picker in its onCreate
Intent phoneContactIntent = new Intent(Intent.ACTION_PICK,
ContactsContract.Contacts.CONTENT_URI);
// Show user only contacts w/ phone numbers
phoneContactIntent.setType(Phone.CONTENT_TYPE);
startActivityForResult(phoneContactIntent, CHOOSE_CONTACT_TO_CALL);
Then, in onActivityResult, it creates a new intent for the main application, with the data that the contact picker returned.
switch (requestCode) {
case (CHOOSE_CONTACT_TO_CALL):
if (resultCode == Activity.RESULT_OK) {
Intent resultIntent = new Intent(this, Home.class);
resultIntent.putExtras(data);
Uri contactData = data.getData();
if (contactData != null)
{
resultIntent.setData(contactData);
}
startActivity(resultIntent);
}
}
finish();
and in my Home class, in onCreate I call getIntent() and inspect the data in the intent that launched the main activity.

Categories

Resources