如何打开安装的Android应用程序与按钮点击意图? [英] How to open a installed android app with a button click intent?
本文介绍了如何打开安装的Android应用程序与按钮点击意图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在开发一个用于教育的机器人启动器,当需要用户点击学校工具按钮时,可以启动安装在设备上的学校工具应用程序。
Hi i'm working on a android launcher for education and I need it to be able to when the user clicks the school tools button it launches the school tools app that is installed on the device
这里是代码
package com.d4a.stzh;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.content.Intent;
import com.actionbarsherlock.app.SherlockFragment;
public class FragmentTab1 extends SherlockFragment {
private Button appbtn;
private Button webbtn;
private Button toolsbttn;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Get the view from fragmenttab1.xml
View view = inflater.inflate(R.layout.fragmenttab1, container, false);
//Get the button from layout
appbtn = (Button) view.findViewById(R.id.app);
webbtn = (Button) view.findViewById(R.id.web);
toolsbttn = (Button) view.findViewById(R.id.tools);
//show all apps installed on the device
appbtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(FragmentTab1.this.getActivity(), MyLauncherActivity.class);
startActivity(intent);
}
});
//luanches google on the default web browser
webbtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String url = "http://www.google.com";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}
});
//tools button i know ths code is wrong!I need help here!
toolsbttn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(FragmentTab1.this.getActivity(), MyLauncherActivity.class);
startActivity(intent);
}
});
return view;
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
setUserVisibleHint(true);
}
}
我还是Android的新手所以请不要判断我
I am still new to android coding so please don't judge me
任何帮助将是惊人的
谢谢你提前
Any help would be amazing Thanks way in advance
请问
Rapsong11
Rapsong11
推荐答案
此代码段应该完全符合您的要求试图实现
This code snippet should do exactly what you are trying to achieve
Intent i;
PackageManager manager = getPackageManager();
try {
i = manager.getLaunchIntentForPackage("com.example.schoolToolApp");
if (i == null)
throw new PackageManager.NameNotFoundException();
i.addCategory(Intent.CATEGORY_LAUNCHER);
startActivity(i);
} catch (PackageManager.NameNotFoundException e) {
}
它将通过其包名称启动另一个应用程序
It will just launch another app by its package name
source - 从您自己的(意图)中打开另一个应用程序
source - Open another application from your own (intent)
这篇关于如何打开安装的Android应用程序与按钮点击意图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文