0

I'm frustrated as to how I can get the function to work properly. I've been researching and looking around about Intents.

At first I thought I got it right but I was wrong, some overview on what I mean. I have made an app with 6 buttons, all of which that open different applications.

  1. Clock, 2. Calendar, 3. Browser, 4. Messaging, 5. Phone, and 6. Contacts.

This is my onClick method for launching the contacts application.

// Contacts Launch
Button contacts_launch = (Button) findViewById(R.id.contacts_launch); 
contacts_launch.setOnClickListener(new View.OnClickListener() { 

 @Override
 public void onClick(View v) {
       Intent intent_contacts = new Intent(Intent.ACTION_MAIN);
       intent_contacts.addCategory(Intent.CATEGORY_LAUNCHER);
       startActivity(intent_contacts);
  }
});

The onClick Intent method is the same for all my buttons, just the intent name has been changed according to the applications name, like messaging is intent_message.

When launching the application, and when I tapped the button. It prompted me with a window where I could select the application. And it ran the app every time I selected the button.

But when I select another application, it launches the contacts app? And doesn't let me choose it like before. How can I fix this? I'm pretty sure I'm using the intent function wrong.

Thanks for your time.

Please check code again, that was my modified one that didn't work which was the one with only one intent method. I added the code that I used at first where it let me choose. That's the one with the intent and category. (The one you can see now)

Kalyan Vedala
  • 1,049
  • 2
  • 17
  • 33
SmallVille
  • 87
  • 8
  • you want to call that page explicitly – Barun May 07 '14 at 11:26
  • I did it that way before, but I think it's better if the user gets to choose what they launch, because when I call the method as an Explicit Intent it only launches one app, and if it's not there it crashes, I am not good with filtering things. – SmallVille May 07 '14 at 11:30
  • on each Button click call that page explicitly.It will work perfectly – Barun May 07 '14 at 11:32
  • use Intent.createChooser with intent which action is set to ACTION_MAIN – pskink May 07 '14 at 11:32
  • @pskink I did it like this Intent intent_contacts = new Intent(Intent.ACTION_MAIN); startActivity(Intent.createChooser(intent_contacts, "View contacts")); Works, but it also lists lots of random apps and activites, but any ways is there any way I can make it to get set as default? So I don't have to keep choosing? – SmallVille May 07 '14 at 11:37

3 Answers3

1

Hi Use the below Code to open contacts:

 @SuppressWarnings("deprecation")
 Intent intent = new Intent(Intent.ACTION_PICK, People.CONTENT_URI);
 startActivity(intent);
Sivakumar
  • 633
  • 4
  • 9
1
Intent sendIntent = new Intent(Intent.ACTION_VIEW);
sendIntent.setData(Uri.parse("sms:"));
sendIntent.putExtra("sms_body", urlToShare);
startActivity(sendIntent);

This is a sample code to open message application or hangout. You can do like this for others also.

Kamalone
  • 4,045
  • 5
  • 40
  • 64
1

if you dont want to select the Activity over and over again (like when using createChooser) try this:

public class Chooser extends Activity implements OnClickListener {
    private static final int NUM = 6;
    private static final CharSequence DEFAULT = "click for select the app, long click to clear it";

    private Intent[] mIntents = new Intent[NUM];
    private LinearLayout mLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mLayout = new LinearLayout(this);
        mLayout.setOrientation(LinearLayout.VERTICAL);
        for (int i = 0; i < NUM; i++) {
            Button b = new Button(this);
            b.setTag(i);
            b.setText(DEFAULT);
            b.setOnClickListener(this);
            registerForContextMenu(b);
            mLayout.addView(b);
        }
        setContentView(mLayout);
    }

    private CharSequence getName(Intent intent) {
        PackageManager mgr = getPackageManager();
        ResolveInfo info = mgr.resolveActivity(intent, 0);
        if (info != null) {
            return info.loadLabel(mgr);
        }
        return intent.getComponent().getShortClassName();
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
        int itemId = (Integer) v.getTag();
        if (mIntents[itemId] != null) {
            menu.add(Menu.NONE, itemId, Menu.NONE, "Clear");
        }
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        int i = item.getItemId();
        Button b = (Button) mLayout.getChildAt(i);
        b.setText(DEFAULT);
        mIntents[i] = null;
        return super.onContextItemSelected(item);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            Button b = (Button) mLayout.getChildAt(requestCode);
            b.setText(getName(data));
            mIntents[requestCode] = data;
            startActivity(data);
        }
    }

    @Override
    public void onClick(View v) {
        int i = (Integer) v.getTag();
        if (mIntents[i] == null) {
            Intent intent = new Intent(Intent.ACTION_PICK_ACTIVITY);
            Intent filter = new Intent(Intent.ACTION_MAIN);
            filter.addCategory(Intent.CATEGORY_LAUNCHER);
            intent.putExtra(Intent.EXTRA_INTENT, filter);
            startActivityForResult(intent, i);
        } else {
            startActivity(mIntents[i]);
        }
    }
}
pskink
  • 23,874
  • 6
  • 66
  • 77
  • what is unclear? its just less than 100 lines of code, mostly ui related so the real thing is starting activity for result – pskink May 08 '14 at 07:24
  • Thanks but, it doesn't save the activity. it opens , and when you go back its there but when you go back to main activity and then click the button again it resets. – SmallVille May 08 '14 at 07:24
  • sure: you have to save it somewhere, see Intent.toUri and Intent.getIntent methods – pskink May 08 '14 at 07:32