Angripa
Angripa

Reputation: 157

Android : Intent crashing the app

I'm new to Android development.
Can anyone tell me in what conditions this code will crash my app?

Please give me references.

Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND); 
sendIntent.putExtra(Intent.EXTRA_TEXT, textMessage); 
sendIntent.setType(HTTP.PLAIN_TEXT_TYPE); // "text/plain" MIME type 
startActivity(sendIntent);

Upvotes: 2

Views: 5183

Answers (5)

Praween Kumar Mishra
Praween Kumar Mishra

Reputation: 823

An implicit intent specifies an action that can invoke any app on the device able to perform the action. Using an implicit intent is useful when your app cannot perform the action, but other apps probably can. If there is more than one application registered that can handle this request, the user will be prompted to select which one to use.

However, it is possible that there are no applications that can handle your intent. In this case, your application will crash when you invoke startActivity(). To avoid this, before calling startActivity() you should first verify that there is at least one application registered in the system that can handle the intent. To do this use resolveActivity() on your intent object:

// Verify that there are applications registered to handle this intent
// (resolveActivity returns null if none are registered)
if (sendIntent.resolveActivity(getPackageManager()) != null) {
    startActivity(sendIntent);
} 

Upvotes: 1

Ganpat Kaliya
Ganpat Kaliya

Reputation: 898

try this,

because of your following line,

sendIntent.setAction(Intent.ACTION_SEND);

you can take permission in manifest.xml file like this,

 <uses-permission android:name="android.permission.SEND_SMS" />

also you can set type text/plain

sendIntent.setType("text/plain");

Upvotes: 1

Ashish Agrawal
Ashish Agrawal

Reputation: 1977

Please look this

 Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plan");
intent.putExtra(Intent.EXTRA_EMAIL, "[email protected]");
intent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
intent.putExtra(Intent.EXTRA_TEXT, "I'm email body.");

startActivity(Intent.createChooser(intent, "Send Email"));

Upvotes: 2

Muhammad Umer Farooq
Muhammad Umer Farooq

Reputation: 26

use permission in your manifest

uses-permission android:name="android.permission.SEND_SMS" 

and set type like below

sendIntent.setType("vnd.android-dir/mms-sms");

Upvotes: 0

KishuDroid
KishuDroid

Reputation: 5451

Create the text message with a string

Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, textMessage);
sendIntent.setType("text/plain");

// Verify that the intent will resolve to an activity
if (sendIntent.resolveActivity(getPackageManager()) != null) {
    startActivity(sendIntent);
}

For binary Data

Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, uriToImage);
shareIntent.setType("image/jpeg");
startActivity(Intent.createChooser(shareIntent, getResources().getText(R.string.send_to)));

Note the following:

You can use a MIME type of "/", but this will only match activities that are able to handle generic data streams.

Upvotes: 2

Related Questions