如何使用 Java/Android 使按钮打开另一个应用程序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4644666/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make a button open another app with Java/Android?
提问by AndroidKen
I am trying to learn to make a simple app that has a few buttons with each opening another app to eliminate the need for another apps. I just can't figure it out. Also, can I place more than one button in this activity to open another app? I can't really find that answer either.
我正在尝试学习制作一个简单的应用程序,它有几个按钮,每个按钮打开另一个应用程序,以消除对其他应用程序的需要。我就是想不通。另外,我可以在这个活动中放置多个按钮来打开另一个应用程序吗?我也真的找不到那个答案。
Button batteryhistory = (Button)findViewById(R.string.BatteryHistoryButtonDialog);
batteryhistory.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent();
ComponentName n = new
ComponentName("com.android.settings",
"com.android.settings.BatteryHistory");
i.setComponent(n);
startActivity(i);
Thanks so much for the help :D
非常感谢您的帮助:D
回答by Sen
Hope this implementation will work :
希望这个实现会起作用:
if (v.getId() == R.id.ImageButton01) {
if (keyCode == KeyEvent.KEYCODE_ENTER) {
// Toast.makeText(this, "Application Name", Toast.LENGTH_LONG).show();
Intent i = new Intent();
i.setAction(Intent.ACTION_VIEW);
i.setClassName("com.xxx.your_package_name",
"com.xxx.your_class_name");
startActivity(i);
}
}
The only thing is that you will have to install the application before hand.
唯一的问题是您必须事先安装该应用程序。
回答by bhavithra
here is the code to open an app(ex whatsapp) from another app
这是从另一个应用程序打开应用程序(ex whatsapp)的代码
public class MainActivity extends Activity{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button bClock = (Button) findViewById(R.id.button1);
bClock.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(Intent.ACTION_MAIN);
PackageManager managerclock = getPackageManager();
i = managerclock.getLaunchIntentForPackage("com.whatsapp");
i.addCategory(Intent.CATEGORY_LAUNCHER);
startActivity(i);
}
});
}
}