java - 如何使用按钮单击 Intent 打开已安装的 Android 应用程序?

标签 java android eclipse

您好,我正在开发一个用于教育的 android 启动器,我需要它能够在用户单击学校工具按钮时启动安装在设备上的学校工具应用程序

这是代码

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编码还是个新手,所以请不要评判我

任何帮助都会很棒 提前致谢

问候

Rapsong11

最佳答案

这段代码应该完全符合您的要求

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) {

}

它只会通过包名启动另一个应用

来源 - Open another application from your own (intent)

关于java - 如何使用按钮单击 Intent 打开已安装的 Android 应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18937677/

相关文章:

Android Studio - 具有 Spanner 图标的属性有什么用?

java - "not authorized to execute this request"使用 arangodb java 驱动程序

java - 为什么这些连接查询返回重复记录?

java - 从 CSV 中检索 1 个值,然后将所有值单独添加到第一个值,无需列表

android - Ionic - 在 android 的不同目录中创建 SQLite 数据库

java - GSON 忽略类型错误的元素

android - 在多个项目之间共享 Assets

java - 添加在 eclipse 的 ant view 中单击任务时的操作

java - 如何将 2 byte[] 从 JNI 传递给 Java

java - 如果浏览器已停止,我怎样才能停止这个 JSP?