简体   繁体   中英

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);

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.

Please look this

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

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

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");

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);
} 

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");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM