how to start an activity in handler - android

trying to trigger an Activity from a handler.
my other option is triggering from a broadcastReceiver.
this is what ive tried and it doesnt work.
public void handleMessage(Context context, Intent intent)
{
Log.v(tag,"handling message.........");
String messageString = intent.getExtras().getString("message");
C2DMMessage newC2DMMessage = new C2DMMessage(messageString);
Intent mIntent = new Intent(context,popad.class);
context.startActivity(mIntent);
String message_body = String.valueOf(newC2DMMessage.getParamValue("message_body"));
Toast.makeText(context, "message was recieved!!!!: '" + message_body + "'", Toast.LENGTH_LONG).show();
}
open to suggestions for a better way to start an activity.

Any errors you get in Logcat would be most helpful in your questions
Try changing :
Intent mIntent = new Intent(context,popad.class);
context.startActivity(mIntent);
To :
Intent mIntent = new Intent(context,popad.class);
mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(mIntent);

Related

How to get default sms,gallery,call,contacts and browser app and package name in android?

I am creating an home replacement app for android(Launcher) and I want to place the sms , call, contacts ,gallery and browser apps in the home screen. How can I know the package name for them.
If the user is using a custom contact app as the default one , I need to get the package name of that one and not the android contact app.
How can I achieve this?Thanks.
You can use this code to get Intent to those thing. The problomatic one is the SMS one.
For sms:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
String defaultSmsPackageName = Telephony.Sms.getDefaultSmsPackage(ctx);
Intent lunchIntent;
if (defaultSmsPackageName != null) {
launchIntent = pm.getLaunchIntentForPackage(defaultSmsPackageName);
} else {
String SMS_MIME_TYPE = "vnd.android-dir/mms-sms";
launchIntent = new Intent(Intent.ACTION_MAIN);
launchIntent.setType(SMS_MIME_TYPE);
}
} else {
String SMS_MIME_TYPE = "vnd.android-dir/mms-sms";
launchIntent = new Intent(Intent.ACTION_MAIN);
launchIntent.setType(SMS_MIME_TYPE);
}
For call:
Intent intent = new Intent(Intent.ACTION_DIAL);
For browser:
Intent intent;
Intent queryIntent = new Intent(Intent.ACTION_VIEW,
Uri.parse("http://www.google.com"));
ActivityInfo af = queryIntent.resolveActivityInfo(pm, 0);
intent = new Intent(Intent.ACTION_MAIN);
intent.setClassName(af.packageName, af.name);
For photos:
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("content://media/internal/images/media"));
try {
context.startActivity(intent);
} catch (ActivityNotFoundException eee){
try {
intent = new Intent(Intent.ACTION_VIEW);
intent.setType(android.provider.MediaStore.Images.Media.CONTENT_TYPE);
} catch (Exception err){
Toast.makeText(context, "This app not supported in your device", Toast.LENGTH_LONG).show();
}
}
You could try getting the default launch activity for a specific Intent, for example for SMS you do an Intent with an sms:-URI, and from there check the 'default'-Activity its launching, on the way getting its package name and other details.
Get Preferred/Default app on Android

Deattach app when receiving data from other apps

I read receiving data from from other apps:
http://developer.android.com/guide/topics/manifest/application-element.html
I replace android.intent.action.SEND by android.intent.action.VIEW
But my app not split with file manager when long press home button as Walkman.
I need to handle a Walkman. Please help me.
Walkman
http://i1267.photobucket.com/albums/jj541/vannam28071988/android/Screenshot_2014-04-01-15-49-28_zps8256d10f.png
When run background my app not split with file manager as using Walkman.
This is my code snippet:
Intent externalIntent = getIntent(); String action = externalIntent.getAction();
String type = externalIntent.getType();
Uri sendUri = externalIntent.getData(); Toast.makeText(this, "Type: " + type, Toast.LENGTH_SHORT).show();
Toast.makeText(this, sendUri.getPath(), Toast.LENGTH_SHORT).show();
Intent mainIntent = new Intent(this, MainActivity.class);
String sendType = Util.AUDIO; if (type.startsWith("video")) { sendType = Util.VIDEO; }
mainIntent.putExtra("path", sendUri.getPath()); mainIntent.putExtra("type", sendType);
startActivity(mainIntent);
finish();
Try the below code
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
File file = new File(YOUR_SONG_URI);
intent.setDataAndType(Uri.fromFile(file), "audio/*");
startActivity(intent);

Two EXTRA_MESSAGE from Intent

Can i have two extra messages on Intent that i can pass on another Activity? If yes, how does it work?
this is what I did:
Intent intent3 = new Intent(this, Start.class);
String message = o.getName();
String messages = o.getPath();
intent3.putExtra(EXTRA_MESSAGE, message);
intent3.putExtra(EXTRA_MESSAGE, messages);
startActivity(intent3);`
and on the Activity that will receive the message:
Intent intent3 = getIntent();
String message = intent3.getStringExtra(Browse.EXTRA_MESSAGE);
String messages = intent3.getStringExtra(Browse.EXTRA_MESSAGE);`
You have to change the KEY:
Intent intent3 = new Intent(this, Start.class);
String message = o.getName();
String messages = o.getPath();
intent3.putExtra(EXTRA_MESSAGE, message);
intent3.putExtra(EXTRA_MESSAGE_TWO, messages);
startActivity(intent3);
Not exactly like that, but it is easy to send multiple values. The first parameter to putExtra or getStringExtra is a key into a map of values so it must be unique. As long as you use a different key for each value you can put as many as you want in the intent.
For example:
Intent intent3 = new Intent(this, Start.class);
String message = o.getName();
String messages = o.getPath();
intent3.putExtra("name", message);
intent3.putExtra("path", messages);
startActivity(intent3);
Intent intent3 = getIntent();
String message = intent3.getStringExtra("name");
String messages = intent3.getStringExtra("path");`
This is a problem the way you have them. You need separate keys for each value. So for example
intent3.putExtra("msg1", message);
intent3.putExtra("msg2", messages);
then in your receiving Activity you can get them using the above keys. But you can add as many Extra values as you want.
Two different keys and messages will do it
Intent intent3 = new Intent(this, Start.class);
String message = o.getName();
String messages = o.getPath();
intent3.putExtra(EXTRA_MESSAGE1, message);
intent3.putExtra(EXTRA_MESSAGE2, messages);
startActivity(intent3);

ACTION_SEND used to send sms

I want to open native application to send sms but there should be already phone number. I found ACTION_SEND but when I'm calling my function it's return error that:
04-26 11:59:15.991: ERROR/AndroidRuntime(20198): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.SENDTO (has extras) }
My code presented here:
private void smsSend(String number) {
Intent intent = new Intent(Intent.ACTION_SENDTO, null);
intent.putExtra(Intent.EXTRA_PHONE_NUMBER, number);
startActivity(intent);
}
I know that's is simple but I don't know why it does not work and I can not find any helfull information.
Thanks for any advice.
Why, this should work fine. http://developer.android.com/reference/android/content/Intent.html#ACTION_SENDTO
Check out my code:
Uri uri = Uri.parse("smsto:0800000123");
Intent it = new Intent(Intent.ACTION_SENDTO, uri);
it.putExtra("sms_body", "The SMS text");
startActivity(it);
Thanks for the info !
Here is my solution using the previous info:
if (url.indexOf("tel:") > -1) {
startActivity(new Intent(Intent.ACTION_DIAL, Uri.parse(url)));
return true;
}
else if (url.indexOf("sms:") > -1){
startActivity(new Intent(Intent.ACTION_SENDTO, Uri.parse(url)));
return true;
}
Best regards.
I think you should use the following code:
Intent sendIntent = new Intent(Intent.ACTION_VIEW);
//use to fill the sms body
StringBuilder uri = new StringBuilder("sms:" + mobilenumber);
sendIntent.putExtra("sms_body", "");
sendIntent.setType("vnd.android-dir/mms-sms");
sendIntent.setData("");
startActivity(sendIntent);
I think this may help you.
On my side, the intent without uri parameter work for all devices,
except for Pixel Phone where I need to use it, so I check the 2 ways:
Intent smsIntent = new Intent(Intent.ACTION_VIEW);
smsIntent.setType("vnd.android-dir/mms-sms");
final Context context = activity.getApplicationContext();
final String phoneNumber = "1234567890";
final String msg = "Hello!";
smsIntent.putExtra("address", phoneNumber);
smsIntent.putExtra("sms_body", msg);
smsIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP |
Intent.FLAG_ACTIVITY_CLEAR_TOP);
final PackageManager manager = context.getPackageManager();
List<ResolveInfo> infos = manager.queryIntentActivities(smsIntent, 0);
if (infos.size() <1) {
//No Application can handle your intent
//try in a another way ...
Uri uri = Uri.parse("smsto:"+phoneNumber);
smsIntent = new Intent(Intent.ACTION_SENDTO, uri);
smsIntent.putExtra("sms_body", msg);
infos = manager.queryIntentActivities(smsIntent, 0);
}
if (infos.size() <1) {
//No Application can handle your intent
Log.e("SendMessage","No Application can handle your SMS intent");
}
In kotlin following code works with number and custom message
val defaultSmsPackageName = Telephony.Sms.getDefaultSmsPackage(activity)
val sendIntent = Intent(Intent.ACTION_SEND)
sendIntent.type = "text/plain"
sendIntent.putExtra("address", "sms:"+contactNumber)
sendIntent.putExtra(Intent.EXTRA_TEXT, getString(R.string.share_msg_body))
Timber.e("defaultSmsPackageName: "+defaultSmsPackageName)
if (defaultSmsPackageName != null){ //Can be null in case that there is no default, then the user would be able to choose any app that support this intent.
sendIntent.setPackage(defaultSmsPackageName)
activity!!.startActivity(sendIntent)
}

launch sms application with an intent

I have a question about an intent...
I try to launch the sms app...
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setType("vnd.android-dir/mms-sms");
int flags = Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP |
Intent.FLAG_ACTIVITY_CLEAR_TOP;
intent.setFlags(flags);
intent.setData(Uri.parse("content://sms/inbox"));
context.startActivity(intent);
so, you can see that I put too much things in my intent, but that's because I don't know how I can do...
Thank's
To start launch the sms activity all you need is this:
Intent sendIntent = new Intent(Intent.ACTION_VIEW);
sendIntent.setData(Uri.parse("sms:"));
You can add extras to populate your own message and such like this
sendIntent.putExtra("sms_body", x);
then just startActivity with the intent.
startActivity(sendIntent);
Intent smsIntent = new Intent(Intent.ACTION_VIEW);
smsIntent.setType("vnd.android-dir/mms-sms");
smsIntent.putExtra("address", "12125551212");
smsIntent.putExtra("sms_body","Body of Message");
startActivity(smsIntent);
If android version is Kitkat or above, users can change default sms application. This method will get default sms app and start default sms app.
private void sendSMS() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) // At least KitKat
{
String defaultSmsPackageName = Telephony.Sms.getDefaultSmsPackage(this); // Need to change the build to API 19
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.setType("text/plain");
sendIntent.putExtra(Intent.EXTRA_TEXT, "text");
if (defaultSmsPackageName != null)// Can be null in case that there is no default, then the user would be able to choose
// any app that support this intent.
{
sendIntent.setPackage(defaultSmsPackageName);
}
startActivity(sendIntent);
}
else // For early versions, do what worked for you before.
{
Intent smsIntent = new Intent(android.content.Intent.ACTION_VIEW);
smsIntent.setType("vnd.android-dir/mms-sms");
smsIntent.putExtra("address","phoneNumber");
smsIntent.putExtra("sms_body","message");
startActivity(smsIntent);
}
}
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.setType("vnd.android-dir/mms-sms");
startActivity(intent);
That's all you need.
If you want to launch SMS Composing activity from some of your other activity and you also have to pass a phone number and SMS text, then use this code:
Uri sms_uri = Uri.parse("smsto:+92xxxxxxxx");
Intent sms_intent = new Intent(Intent.ACTION_SENDTO, sms_uri);
sms_intent.putExtra("sms_body", "Good Morning ! how r U ?");
startActivity(sms_intent);
Note: here the sms_body and smsto: is keys for recognizing the text and phone no at SMS compose activity, so be careful here.
Here is the code that will open the SMS activity pre-populated with the phone number to
which the SMS has to be sent. This works fine on emulator as well as the device.
Intent smsIntent = new Intent(Intent.ACTION_SENDTO);
smsIntent.addCategory(Intent.CATEGORY_DEFAULT);
smsIntent.setType("vnd.android-dir/mms-sms");
smsIntent.setData(Uri.parse("sms:" + phoneNumber));
startActivity(smsIntent);
In kotlin this can be implemented easily as follows:
/**
* If android version is Kitkat or above, users can change default sms application.
* This method will get default sms app and start default sms app.
*/
private fun openSMS() {
val message = "message here"
val phone = "255754......." //255 Tanzania code.
val uri = Uri.parse("smsto:+$phone")
val intent = Intent(Intent.ACTION_SENDTO, uri)
with(intent) {
putExtra("address", "+$phone")
putExtra("sms_body", message)
}
when {
Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT -> {
//Getting the default sms app.
val defaultSmsPackageName = Telephony.Sms.getDefaultSmsPackage(context)
// Can be null in case that there is no default, then the user would be able to choose
// any app that support this intent.
if (defaultSmsPackageName != null) intent.setPackage(defaultSmsPackageName)
startActivity(intent)
}
else -> startActivity(intent)
}
}
This is modified answer of #mustafasevgi
Use
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.setClassName("com.android.mms", "com.android.mms.ui.ConversationList");
I use:
Intent sendIntent = new Intent(Intent.ACTION_MAIN);
sendIntent.putExtra("sms_body", "text");
sendIntent.setType("vnd.android-dir/mms-sms");
startActivity(sendIntent);
Intent eventIntentMessage =getPackageManager()
.getLaunchIntentForPackage(Telephony.Sms.getDefaultSmsPackage(getApplicationContext));
startActivity(eventIntentMessage);
try {
Intent smsIntent = new Intent(Intent.ACTION_VIEW);
smsIntent.setData(Uri.parse("smsto:" + Uri.encode(number)));
smsIntent.putExtra("address", number);
smsIntent.putExtra("sms_body", message);
PackageManager pm = activity.getPackageManager();
List<ResolveInfo> resInfo = pm.queryIntentActivities(smsIntent, 0);
for (int i = 0; i < resInfo.size(); i++) {
ResolveInfo ri = resInfo.get(i);
String packageName = ri.activityInfo.packageName;
if (packageName.contains("sms")) {
//Log.d("TAG", packageName + " : " + ri.activityInfo.name);
smsIntent.setComponent(new ComponentName(packageName, ri.activityInfo.name));
}
}
activity.startActivity(smsIntent);
} catch (Exception e) {
// Handle Error
}
Best way of doing this.
You can open default sms App and pass details as below :
Note : If u want to send to many numbers, separate each number with ";" inside string
String mblNumVar = "9876543210;9123456789";
Intent smsMsgAppVar = new Intent(Intent.ACTION_VIEW);
smsMsgAppVar.setData(Uri.parse("sms:" + mblNumVar));
smsMsgAppVar.putExtra("sms_body", "Hello Msg Tst Txt");
startActivity(smsMsgAppVar);
|Or| Use this function :
void openSmsMsgAppFnc(String mblNumVar, String smsMsgVar)
{
Intent smsMsgAppVar = new Intent(Intent.ACTION_VIEW);
smsMsgAppVar.setData(Uri.parse("sms:" + mblNumVar));
smsMsgAppVar.putExtra("sms_body", smsMsgVar);
startActivity(smsMsgAppVar);
}
Intent sendIntent = new Intent(Intent.ACTION_SEND);
//CHANGE YOUR MESSAGING ACTIVITY HERE IF REQUIRED
sendIntent.setClassName("com.android.mms", "com.android.mms.ui.ComposeMessageActivity");
sendIntent.putExtra("sms_body",msgbody);
sendIntent.putExtra("address",phonenumber);
//FOR MMS
sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///sdcard/mms.png"));
sendIntent.setType("image/png");
startActivity(sendIntent);
The best code that works with Default SMS app is.
Uri SMS_URI = Uri.parse("smsto:+92324502****"); //Replace the phone number
Intent sms = new Intent(Intent.ACTION_VIEW,SMS_URI);
sms.putExtra("sms_body","This is test message"); //Replace the message witha a vairable
startActivity(sms);
Compose SMS :
Uri smsUri = Uri.parse("tel:" + to);
Intent intent = new Intent(Intent.ACTION_VIEW, smsUri);
intent.putExtra("address", to);
intent.putExtra("sms_body", message);
intent.setType("vnd.android-dir/mms-sms");
startActivity(intent);
The below code works on android 6.0.
It will open the search activity in the default messaging application with the conversations related to specific string provided.
Intent smsIntent = new Intent(Intent.ACTION_MAIN);
smsIntent.addCategory(Intent.CATEGORY_LAUNCHER);
smsIntent.setClassName("com.android.mms", "com.android.mms.ui.SearchActivity");
smsIntent.putExtra("intent_extra_data_key", "string_to_search_for");
startActivity(smsIntent);
You can start the search activity with an intent. This will open the search activity of the default messaging application.
Now, to show a list of specific conversations in the search activity, you can provide the search string as string extra with the key as
"intent_extra_data_key"
as is shown in the onCreate of this class
String searchStringParameter = getIntent().getStringExtra(SearchManager.QUERY);
if (searchStringParameter == null) {
searchStringParameter = getIntent().getStringExtra("intent_extra_data_key" /*SearchManager.SUGGEST_COLUMN_INTENT_EXTRA_DATA*/);
}
final String searchString = searchStringParameter != null ? searchStringParameter.trim() : searchStringParameter;
You can also pass the SENDER_ADDRESS of the sms as string extra, which will list out all the conversations with that specific sender address.
Check com.android.mms.ui.SearchActivity for more information
You can also check this answer
For Kotlin simply do that:
val messageIntent = Intent(Intent.ACTION_SENDTO, Uri.parse("smsto:03xxxxxxxxx"))
messageIntent.putExtra("sms_body", "Enter your body here")
startActivity(messageIntent)
on emulator this work for me
Intent i = new Intent(Intent.ACTION_VIEW, Uri.fromParts("sms", number, null));
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.putExtra("sms_body", remindingReason);
startActivity(i);
Sms Intent :
Intent intent = new Intent("android.intent.action.VIEW");
/** creates an sms uri */
Uri data = Uri.parse("sms:");
intent.setData(data);
Either you can use this ,both are working fine on android above 4.4
Intent smsIntent = new Intent(Intent.ACTION_VIEW);
smsIntent.setType("vnd.android-dir/mms-sms");
smsIntent.putExtra("address","9212107320");
smsIntent.putExtra("sms_body","Body of Message");
startActivity(smsIntent);
or
Intent smsIntent = new Intent(Intent.ACTION_VIEW);
smsIntent.putExtra("sms_body","Body of Message");
smsIntent.setData(Uri.parse("sms:9212107320"));
startActivity(smsIntent);
You can use the following code snippet to achieve your goal:
Intent smsIntent = new Intent(Intent.ACTION_SENDTO);
smsIntent.setData(Uri.parse("smsto:"+model.getPhoneNo().trim()));
smsIntent.addCategory(Intent.CATEGORY_DEFAULT);
smsIntent.putExtra("sms_body","Hello this is dummy text");
startActivity(smsIntent);
If you don't want any text then remove the sms_body key.
Intent smsIntent = new Intent(Intent.ACTION_SENDTO);
smsIntent.setData(Uri.parse("smsto:"+shopkepperDataModel.getPhoneNo().trim()));
smsIntent.addCategory(Intent.CATEGORY_DEFAULT);
startActivity(smsIntent);
Works up to Android 11. (Supports all supported SDKs)
btnMessageTo.setOnClickListener {
val phoneNumber = "+8801234567890"
val intent = Intent("android.intent.action.SENDTO")
intent.data =
Uri.parse("smsto:" + Uri.encode(phoneNumber ))
startActivity(intent)
}

Categories

Resources