I try the following code to send messages to line app. It works; however, before I send message,it will move to the line friends page and I have to choose friends whom I want to send the messages to. How could I modify the code that I could choose friends at code instead of choosing friends manually.
public class MainActivity extends AppCompatActivity {
static final int REQUEST_ACTION_PICK = 1;
public static final String PACKAGE_NAME = "jp.naver.line.android";
public static final String CLASS_NAME = "jp.naver.line.android.activity.selectchat.SelectChatActivity";
private List<ApplicationInfo> m_appList;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sendTextHandler(this);
}
public void sendTextHandler(MainActivity view) {
String sendText = ((TextView)findViewById(R.id.send_text)).getText().toString();
if(checkLineInstalled()){
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setClassName(PACKAGE_NAME, CLASS_NAME);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, sendText);
startActivity(intent);
}else{
Toast toast = Toast.makeText(this, "LINEがインストールされていません", Toast.LENGTH_SHORT);
toast.show();
}
}
private boolean checkLineInstalled(){
PackageManager pm = getPackageManager();
m_appList = pm.getInstalledApplications(0);
boolean lineInstallFlag = false;
for (ApplicationInfo ai : m_appList) {
if(ai.packageName.equals(PACKAGE_NAME)){
lineInstallFlag = true;
break;
}
}
return lineInstallFlag;
}
}
The code is from https://gist.github.com/ekos/3993270.
If you want to be able to open a specific user based on the ID you can do the following:
String userId = findUserId();
String sendText = "line://ti/p/~" + userId;
Intent intent = null;
try {
intent = Intent.parseUri(sendText, Intent.URI_INTENT_SCHEME);
} catch (URISyntaxException e) {
e.printStackTrace();
}
startActivity(intent);
If you want to make a message and then be able to choose who to send it to you can use the following:
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setData(Uri.parse("line://msg/text/" + getMessage()));
startActivity(intent);
Related
Below 30 API there were two methods I could use to check if intent can be handled by some app activity:
fun Intent.canBeHandled(packageManager: PackageManager): Boolean {
return resolveActivity(packageManager) != null
}
fun PackageManager.isIntentCanBeHandled(intent: Intent): Boolean {
val infos = queryIntentActivities(intent, 0)
return infos.isNotEmpty()
}
But when I test on Pixel 3 API 30 Emulator it doesn't work as expected, e.g. queryIntentActivities() returns usually 0 or 1 (e.g. 1 for send intent when it should have been 7, as it was with API 29-)
Basically Intent.createChooser(intent, "") works correctly and suggests 2 apps (for example) but ext funcs Intent.canBeHandled() & PackageManager.isIntentCanBeHandled() return false
Intent needs to be created as a chooser - Please refer to last few lines of following example:
public class MainActivity extends AppCompatActivity {
private static final String cstLogTag = "Implicit Intents";
private enum enuShare {shareCompat, shareSheet, intentResolver}
#Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void buttonClicked(View view) {
EditText editText;
String strText, strButton;
Uri uri;
Intent chooser, intent;
chooser = intent = null;
switch (view.getId()) {
case R.id.btnWebsite:
strButton = "btnWebsite";
editText = findViewById(R.id.edtWebsite);
strText = editText.getText().toString();
uri = Uri.parse(strText);
intent = new Intent(Intent.ACTION_VIEW, uri);
break;
case R.id.btnLocation:
strButton = "btnLocation";
editText = findViewById(R.id.edtLocation);
strText = editText.getText().toString();
uri = Uri.parse("geo:0,0?q=" + strText);
intent = new Intent(Intent.ACTION_VIEW, uri);
break;
case R.id.btnShareText:
strButton = "btnShareText";
editText = findViewById(R.id.edtShareText);
strText = editText.getText().toString();
intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, strText);
intent.putExtra(Intent.EXTRA_SUBJECT, "Just Testing");
break;
default:
Log.d(cstLogTag, "Button not recognised");
return;
}
//intent.resolveActivity(getPackageManager()) returns null so...
chooser = Intent.createChooser(intent, "Choose");
if(chooser.resolveActivity(getPackageManager()) == null)
Log.d(cstLogTag, "Unresolved activity for " + strButton);
else
startActivity(chooser);
}
}
Using this code only open particulat number's chat but Text is not share.How can I do this?
public class MainActivity extends AppCompatActivity {
Button Wa;
String id = "+919000000000";
EditText txt;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt = (EditText)findViewById(R.id.editText);
Wa = (Button)findViewById(R.id.btn_whatsapp);
Wa.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Uri uri = Uri.parse("smsto:" + id);
Intent waIntent = new Intent(Intent.ACTION_SENDTO,uri);
String text = "testing message";
waIntent.setPackage("com.whatsapp");
if (waIntent != null) {
waIntent.putExtra(Intent.EXTRA_TEXT, text);
startActivity(Intent.createChooser(waIntent, text));
} else {
Toast.makeText(getApplicationContext(), "WhatsApp not found", Toast.LENGTH_SHORT)
.show();
}
}
});
}
Since you trying to achieve it as "smsto:", "text/plain" as type will help you. Try Extra as "sms_body" if it won't help.
Uri uri = Uri.parse("smsto:" + id);
Intent waIntent = new Intent(Intent.ACTION_SENDTO,uri);
String text = "testing message";
waIntent.setPackage("com.whatsapp");
if (waIntent != null) {
waIntent.setType("text/plain");
//waIntent.putExtra(Intent.EXTRA_TEXT, text);
waIntent.putExtra("sms_body", text);
startActivity(Intent.createChooser(waIntent, text));
} else {
Toast.makeText(getApplicationContext(), "WhatsApp not found", Toast.LENGTH_SHORT)
.show();
}
Please go through this stackoverflow links
Sending message through WhatsApp
Send text to specific contact
It seems that WhatsApp still does't have supported this fetaure.
You can only open chat history for particular number using below code
try {
Uri mUri = Uri.parse("smsto:+98xxxxxxxx");
Intent mIntent = new Intent(Intent.ACTION_SENDTO, mUri);
mIntent.setPackage("com.whatsapp");
// mIntent.putExtra("sms_body", "My body");
mIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
startActivity(mIntent);
} catch (Exception e) {
// alert WhatsApp in not installed
}
Answer from Here
Earlier it wasn't possible but since the May '15 update. Checkout :
try{
PackageInfo info = pm.getPackageInfo("com.whatsapp", PackageManager.GET_META_DATA);
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
String sendString = "some random string";
sendIntent.setPackage("com.whatsapp");
sendIntent.putExtra(Intent.EXTRA_TEXT, sendString);
sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
sendIntent.setType("image/*");
startActivity(sendIntent);
} catch (Exception e){
// some code
}
Here PackageInfo line is just to check if WhatsApp is installed. It throws Exception if not. You can just ignore that if you want to do a normal share (and setPackage also).
Also. It is important that the media you want to share has to be publicly available on local storage.
UPDATE
To send to a specific contact
Uri uri = Uri.parse("smsto:" + "<CONTACT_NUMBER>");
Intent i = new Intent(Intent.ACTION_SENDTO, uri);
i.putExtra(Intent.EXTRA_TEXT, whatsAppMessage);
As Action Send To is now allowed.
Try something like this:
public class MainActivity extends AppCompatActivity {
Button Wa;
String id = "+919000000000";
EditText txt;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt = (EditText)findViewById(R.id.editText);
Wa = (Button)findViewById(R.id.btn_whatsapp);
Wa.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
PackageManager pm=getPackageManager();
try {
Uri uri = Uri.parse("smsto:" + number);
Intent i = new Intent(Intent.ACTION_SENDTO, uri);
waIntent.setType("text/plain");
String text = "testing message";
PackageInfo info=pm.getPackageInfo("com.whatsapp", PackageManager.GET_META_DATA);
waIntent.setPackage("com.whatsapp");
waIntent.putExtra(Intent.EXTRA_TEXT, text);
startActivity(Intent.createChooser(waIntent, text));
}
catch (NameNotFoundException e) {
Toast.makeText(this, "WhatsApp not found", Toast.LENGTH_SHORT).show();
}
}
});
}
}
Here is my code to send Danger SMS and Email.. I want to send video or photos along with this. How to do that??
public class TriggerNotifManager {
private static final String MESSGAE_BODY = "in danger.Location is : ";;
private static final String VCid = "noreplyvcare#gmail.com";
private static final String VCpass = "vcx2015+";
private static final String subject = "I am in Danger";
private static final String noReply = "This is a system generated email. Please Contact the person in danger."
+"\n"+"Thank You,\n"+"VCare";
List<Contact> contactList;
GetLocationManager myLoc;
SendSMSManager mySMS;
SendMailManager myEmail;
String name,latlong,address;
public TriggerNotifManager(String name,List<Contact> contactList,GetLocationManager myLoc){
this.contactList=contactList;
this.myLoc=myLoc;
mySMS=new SendSMSManager();
myEmail=new SendMailManager();
latlong=myLoc.getLatLong();
address=myLoc.getCompleteAddress();
this.name=name;
}
public void sendDangerSMS(){
for(int i=0;i<contactList.size();i++)
{
String phone=(contactList.get(i)).getPhoneNumber();
mySMS.sendSMSMessage(phone,name+" : HELP !! "+"I am "+MESSGAE_BODY+latlong+"\n"+address);
}
}
public void sendDangerMail(){
for(int i=0;i<contactList.size();i++)
{
String recipient=(contactList.get(i)).getEmail();
// {senderID,senderPass,subject,body,recipientID}
myEmail.execute(VCid,VCpass,subject,"HELP !! "+name+" is "+MESSGAE_BODY+latlong+"\n"+address+"\n"+noReply,recipient);
}
}
public void sendSafeSMS(){
for(int i=0;i<contactList.size();i++)
{
String phone=(contactList.get(i)).getPhoneNumber();
mySMS.sendSMSMessage(phone,name+" : I am Safe ");
}
}
}
Here is my Alert page code.. In this I launch camera in MenuOption.. It takes Photos but i don't know how to send it. Please suggest code for doing this
public void onAlertClick(View v)
{
myNotif.sendDangerSMS();
myNotif.sendDangerMail();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.alert, menu);//Menu Resource, Menu
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.item1:
Uri uri2 = Uri.parse("https://www.google.co.in/search?q=police%20stations+near+me");
Intent i2 = new Intent(Intent.ACTION_VIEW, uri2);
startActivity(i2);
break;
case R.id.item2:
Uri uri1 = Uri.parse("https://www.google.co.in/search?q=hospitals+near+me");
Intent i1 = new Intent(Intent.ACTION_VIEW, uri1);
startActivity(i1);
break;
case R.id.item3:
Intent i3 = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivity(i3);
default:
break;
}
return true;
}
}
Use the below code to sent a mail
String filelocation="/mnt/sdcard/contacts_sid.vcf";
Intent emailIntent = new Intent(Intent.ACTION_SEND);
// set the type to 'email'
emailIntent .setType("vnd.android.cursor.dir/email");
String to[] = {"asd#gmail.com"};
emailIntent .putExtra(Intent.EXTRA_EMAIL, to);
// the attachment
emailIntent .putExtra(Intent.EXTRA_STREAM, filelocation);
// the mail subject
emailIntent .putExtra(Intent.EXTRA_SUBJECT, "Subject");
startActivity(Intent.createChooser(emailIntent , "Send email..."));
Answer link here: How to send an email with a file attachment in Android
I am trying to create a class in java which manages different social sharing apps. The class is based on android intents.
but when I try to execute Telegram intent, it doesn't find the app.
Here I put the code I have written:
public void shareTelegram(String message)
{
Intent waIntent = new Intent(Intent.ACTION_SEND);
waIntent.setType("text/plain");
waIntent.setPackage("com.telegram");
if (waIntent != null) {
waIntent.putExtra(Intent.EXTRA_TEXT, message);//
_androidActivity.startActivity(Intent.createChooser(waIntent, "Share with"));
}
else
{
Toast.makeText(_androidActivity.getApplicationContext(), "Telegram is not installed", Toast.LENGTH_SHORT).show();
}
}
Where could I find the package name?
Thanks in advance.
All Android app have an unique ID, market ID. If you look into Google Play or google search market://details?id=org.telegram, It send you to
https://play.google.com/store/apps/details?id=org.telegram.messenger
If you send the intent with:
waIntent.setPackage("org.telegram.messenger");
It will work.
If you prefer a little bit complex system I recommend you to use:
/**
* Intent to send a telegram message
* #param msg
*/
void intentMessageTelegram(String msg)
{
final String appName = "org.telegram.messenger";
final boolean isAppInstalled = isAppAvailable(mUIActivity.getApplicationContext(), appName);
if (isAppInstalled)
{
Intent myIntent = new Intent(Intent.ACTION_SEND);
myIntent.setType("text/plain");
myIntent.setPackage(appName);
myIntent.putExtra(Intent.EXTRA_TEXT, msg);//
mUIActivity.startActivity(Intent.createChooser(myIntent, "Share with"));
}
else
{
Toast.makeText(mUIActivity, "Telegram not Installed", Toast.LENGTH_SHORT).show();
}
}
And check if is installed with:
/**
* Indicates whether the specified app ins installed and can used as an intent. This
* method checks the package manager for installed packages that can
* respond to an intent with the specified app. If no suitable package is
* found, this method returns false.
*
* #param context The application's environment.
* #param appName The name of the package you want to check
*
* #return True if app is installed
*/
public static boolean isAppAvailable(Context context, String appName)
{
PackageManager pm = context.getPackageManager();
try
{
pm.getPackageInfo(appName, PackageManager.GET_ACTIVITIES);
return true;
}
catch (NameNotFoundException e)
{
return false;
}
}
For opening telegram channel :
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse("http://telegram.me/shes_ir"));
final String appName = "org.telegram.messenger";
try {
if (isAppAvailable(mainActivity.getApplicationContext(), appName))
i.setPackage(appName);
} catch (PackageManager.NameNotFoundException e) {}
mainActivity.startActivity(i);
> **//open telegram directly without intent to specify id.**
Intent telegram = new Intent(android.content.Intent.ACTION_SEND);
telegram.setData(Uri.parse("http://telegram.me/myId"));
telegram.setPackage("org.telegram.messenger");
Test.this.startActivity(Intent.createChooser(telegram, "Share with"));
void intentMessageTelegram(String msg)
{
final String appName = "org.telegram.messenger";
final boolean isAppInstalled = isAppAvailable(this.getApplicationContext(), appName);
if (isAppInstalled)
{
Intent myIntent = new Intent(Intent.ACTION_SEND);
myIntent.setType("text/plain");
myIntent.setPackage(appName);
myIntent.putExtra(Intent.EXTRA_TEXT, msg);//
this.startActivity(Intent.createChooser(myIntent, "Share with"));
}
else
{
Toast.makeText(this, "Telegram not Installed", Toast.LENGTH_SHORT).show();
}
}
public static boolean isAppAvailable(Context context, String appName)
{
PackageManager pm = context.getPackageManager();
try
{
pm.getPackageInfo(appName, PackageManager.GET_ACTIVITIES);
return true;
}
catch (Exception e)
{
return false;
}
}
btnSend.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
intentMessageTelegram("Hi");
}
});
In case you wish to just open the chat with some phone number, it's as such:
/**#param fullPhoneNumber universal phone number, meaning including "+" and the country phone prefix */
fun getTelegramChatIntentFromPhoneNumber(fullPhoneNumber: String): Intent {
val uri =
Uri.Builder().scheme("http").authority("telegram.me").appendEncodedPath(fullPhoneNumber).build()
return Intent(Intent.ACTION_VIEW, uri).setPackage("org.telegram.messenger")
}
Note that you can remove the "setPackage" part in case you wish to support all apps that can handle it.
A similar thing is available for WhatsApp, BTW:
fun prepareWhatsAppMessageIntent(fullPhoneNumber: String, message: String? = null): Intent {
val builder = Uri.Builder().scheme("https").authority("api.whatsapp.com").path("send")
builder.appendQueryParameter("phone", fullPhoneNumber)
message?.let { builder.appendQueryParameter("text", it) }
return Intent(Intent.ACTION_VIEW, builder.build())
}
I know this type of question has been answered many a times on SO. But I am unable to locate an answer to suit my requirement.
I need to know if Adobe Reader for Android is installed on the client. If not trigger an Installation of the Reader from the application and then view the pdf...
I am sort of a newbie on Android...Flow with the code is appreciated..
Thank U..
Hi Kunal i have written some code for you please cross verify it because i havent tested it
public static final String QUICK_OFFICE_URL = "https://market.android.com/search?q=quick+office&so=1&c=apps";
public static final String QUICK_OFFICE_TRIAL_PACKAGE = "com.qo.android.am3.trial";
public static final String QUICK_OFFICE_PACKAGE = "com.qo.android.am3";
public static final String ADOBE_READER_URL="https://market.android.com/search?q=adobe+reader&so=1&c=apps";
public static final String ADOBE_READER_PACKAGE="com.adobe.reader";
public static final String DOCUMENTS_TO_GO_PACKAGE = "com.dataviz.docstogo";
public static final String GOOGLE_DOCUMENT_PACKAGE ="com.google.android.apps.docs";
public static final String OPEN_OFFICE_PACKAGE = "at.tomtasche.reader";
public static final String BEAMREADER_PACKAGE = "com.slgmobile.beamreader";
public static final String PDFVIEWER_PACKAGE = "mobi.mgeek.pdfviewer";
public static final String ANDROID_PDF_VIEWER_PACKAGE = "net.sf.andpdf.pdfviewer";
public static final String EZ_READER = "udk.android.reader";
public static final String PDF_VIEWER = "com.dzepina.pdfviewer";
private void openFileForReading(File file, String extension) {
Log.v(TAG, " openFileForReading(File file, String extension)");
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
if (extension.equalsIgnoreCase("pdf")) {
Log.v(TAG, " file extension is .pdf");
if (appInstalledOrNot(Common.ADOBE_READER_PACKAGE)
|| appInstalledOrNot(Common.BEAMREADER_PACKAGE)
|| appInstalledOrNot(Common.PDF_VIEWER)
|| appInstalledOrNot(Common.PDFVIEWER_PACKAGE)
|| appInstalledOrNot(Common.EZ_READER)) {
intent.setDataAndType(Uri.fromFile(file), "application/*");
startActivity(intent);
} else {
showAlertDialog(Common.ADOBE_READER_URL, file);
}
} else if (extension.equalsIgnoreCase("doc")) {
Log.v(TAG, " file extension is .doc");
if (appInstalledOrNot(Common.QUICK_OFFICE_PACKAGE)
|| appInstalledOrNot(Common.DOCUMENTS_TO_GO_PACKAGE)
|| appInstalledOrNot(Common.GOOGLE_DOCUMENT_PACKAGE)
|| appInstalledOrNot(Common.OPEN_OFFICE_PACKAGE)) {
intent.setDataAndType(Uri.fromFile(file), "application/*");
startActivity(intent);
} else {
showAlertDialog(Common.QUICK_OFFICE_URL, file);
}
} else if (extension.equalsIgnoreCase("docx")) {
Log.v(TAG, " file extension is .docx");
if (appInstalledOrNot(Common.QUICK_OFFICE_PACKAGE)
|| appInstalledOrNot(Common.DOCUMENTS_TO_GO_PACKAGE)
|| appInstalledOrNot(Common.GOOGLE_DOCUMENT_PACKAGE)
|| appInstalledOrNot(Common.OPEN_OFFICE_PACKAGE)) {
intent.setDataAndType(Uri.fromFile(file), "application/*");
startActivity(intent);
} else {
showAlertDialog(Common.QUICK_OFFICE_URL, file);
}
} else {
intent.setDataAndType(Uri.fromFile(file), "application/*");
startActivity(intent);
}
}
private void showAlertDialog(final String uri, final File file) {
AlertDialog.Builder dialog = new AlertDialog.Builder(
DocumentsActivity.this);
dialog.setTitle("No Application Found !!!");
dialog.setMessage("Install App From Market");
dialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface arg0, int arg1) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(uri));
startActivity(intent);
}
});
dialog.setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int whichButton) {
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "application/*");
startActivity(intent);
dialog.cancel();
}
});
dialog.show();
}
private boolean appInstalledOrNot(String packageName) {
PackageManager pm = getPackageManager();
boolean app_installed = false;
try {
pm.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES);
app_installed = true;
} catch (PackageManager.NameNotFoundException e) {
app_installed = false;
}
return app_installed;
}
If you want to link to the market if the Reader is not installed you can fire this Intent in your Activity:
Intent marketIntent = new Intent(Intent.ACTION_VIEW)
.setData(Uri.parse("market://details?id=com.adobe.reader"));
startActivity(marketIntent);
Viewing a PDF is a bit more tricky:
Intent showPDFIntent = new Intent();
showPDFIntent.setPackage("com.adobe.reader");
showPDFIntent.setDataAndType(Uri.fromFile(myPDFFile), "application/pdf");
startActivity(showPDFIntent);
I hope this helps.
Best wishes,
Tim