如何从内置Web浏览器中的代码而不是在我的应用程序中打开URL?
我试过这个:
try {
Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(download_link));
startActivity(myIntent);
} catch (ActivityNotFoundException e) {
Toast.makeText(this, "No application can handle this request."
+ " Please install a webbrowser", Toast.LENGTH_LONG).show();
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
但我有一个例外:
No activity found to handle Intent{action=android.intent.action.VIEW data =www.google.com
Run Code Online (Sandbox Code Playgroud) 我正在尝试从Google Play安装应用.我可以理解,在打开Google Play商店网址时,它会打开Google Play,当我按下后退按钮时,活动会恢复.
Intent marketIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(appURL));
marketIntent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
startActivity(marketIntent);
Run Code Online (Sandbox Code Playgroud)
当我回到活动时,我试着调用它onResume()来检查应用程序是否已安装,但是我收到错误:
@Override
protected void onResume() {
super.onResume();
boolean installed = false;
while (!installed) {
installed = appInstalledOrNot(APPPACKAGE);
if (installed) {
Toast.makeText(this, "App installed", Toast.LENGTH_SHORT).show();
}
}
}
private boolean appInstalledOrNot(String uri) {
PackageManager pm = getPackageManager();
boolean app_installed = false;
try {
pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
app_installed = true;
}
catch (PackageManager.NameNotFoundException e) {
app_installed = false;
}
return app_installed ;
}
Run Code Online (Sandbox Code Playgroud)
错误如下:
E/AndroidRuntime(796):java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.appinstaller/com.example.appinstaller.MainActivity}:android.content.ActivityNotFoundException:找不到处理Intent的活动{act = …
我想知道如何将文本发送到特定的WhatsApp联系人.我找到了一些代码来查看特定联系人,但不发送数据.
Cursor c = getContentResolver().query(ContactsContract.Data.CONTENT_URI,
new String[] { ContactsContract.Contacts.Data._ID }, ContactsContract.Data.DATA1 + "=?",
new String[] { id }, null);
c.moveToFirst();
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("content://com.android.contacts/data/" + c.getString(0)));
startActivity(i);
c.close();
Run Code Online (Sandbox Code Playgroud)
这适用于查看whatsapp-contact,但我现在如何添加一些文本?或者Whatsapp开发人员没有实现这样的api?
是否可以whatsapp直接从另一个应用程序向特定联系人发送消息?我知道联系人ID.我不想whatsapp通过Intent 打开.我只想像普通的短信一样直接发送消息.
我已尝试在stackoverflow上发布其他解决方案,但它们不适合我.
我想打开WhatsApp一个特定的对话,并用一些字符串填充文本字段.
我拥有的代码和我设法与联系人打开对话:
private void openConversationWithWhatsapp(String e164PhoneNumber){
String whatsappId = e164PhoneNumber+"@s.whatsapp.net";
Uri uri = Uri.parse("smsto:" + whatsappId);
Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
intent.setPackage("com.whatsapp");
intent.putExtra(Intent.EXTRA_TEXT, "text");
intent.putExtra(Intent.EXTRA_SUBJECT, "subject");
intent.putExtra(Intent.EXTRA_TITLE, "title");
intent.putExtra(Intent.EXTRA_EMAIL, "email");
intent.putExtra("sms_body", "The text goes here");
intent.putExtra("text","asd");
intent.putExtra("body","body");
intent.putExtra("subject","subjhect");
startActivity(intent);
}
Run Code Online (Sandbox Code Playgroud)
但是文本框中没有内容.我试图查看AndroidManifest.xml文件内部并找到有关其对话活动的以下信息:
<activity android:theme="@style/Theme.App.CondensedActionBar" android:name="com.whatsapp.Conversation" android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" android:windowSoftInputMode="stateUnchanged">
<intent-filter>
<action android:name="android.intent.action.SENDTO" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="sms" />
<data android:scheme="smsto" />
</intent-filter>
</activity>
Run Code Online (Sandbox Code Playgroud)
有人知道额外使用吗?他们故意阻止这个吗?我在他们的FAQ页面中看到了他们的iOS API .
我想将一些预先填充的文本消息发送给特定的用户/用户,我做了谷歌,我遇到了一些类似的堆栈溢出问题,如
发送消息给特定号码whatsapp android? 发送消息给特定号码whatsapp android?
和
Android:如何通过使用什么应用程序以编程方式发送消息,我们聊天? Android:如何通过使用什么应用程序以编程方式发送消息,我们聊天?
但我无法获得预期任务的相关解决方案.
任何帮助将不胜感激 .
使用此代码只打开特殊号码的聊天但文本不是共享.我该怎么做?
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();
}
}
});
}
Run Code Online (Sandbox Code Playgroud) 这就是我调用SMS应用程序的方式:
Intent sendIntent = new Intent(Intent.ACTION_VIEW);
sendIntent.putExtra("sms_body", "The SMS text");
sendIntent.setType("vnd.android-dir/mms-sms");
startActivity(sendIntent);
Run Code Online (Sandbox Code Playgroud)
如何通过twitter/Whatsapp/Facebook发送消息呢?我该怎么写代替mms-sms?我没有发现这方面的文件.
我在StackOverflow中经历了大量的WhatsApp帖子.
像这样: 使用WhatsAPI是否合法?
我的问题是这个.我设法从我的应用程序向WhatsApp发送一条消息给我的联系人列表中的某个人.
但是,我想通过WhatsApp向不在我的联系人列表中的人发送消息(不是垃圾邮件!),而且我无法使用给定的解决方案.
这怎么可能?
顺便说一下,如何用预先定义的消息填充WhatsApp文本字段的主体,以便用户可以立即编辑或发送?"sms_body"或Intent.EXTRA_TEXT似乎不起作用......
public void shareWhatsApp(String whatsappid) {
try {
Cursor c = getContentResolver().query(ContactsContract.Data.CONTENT_URI,
new String[] { ContactsContract.Contacts.Data._ID }, ContactsContract.Data.DATA1 + "=?",
new String[] { whatsappid }, null);
c.moveToFirst();
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("content://com.android.contacts/data/" + c.getString(0)));
i.putExtra(Intent.EXTRA_TEXT, "Hello!");
startActivity(i);
c.close();
} catch (Exception e) {
Toast.makeText(this, "Install WhatsApp First", Toast.LENGTH_LONG).show();;
e.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)