如何通过点击按钮意图打开已安装的Android应用程序?

7

你好,我正在开发一款针对教育领域的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);
    }

}

我对安卓编程还很陌生,所以请不要评判我。

任何帮助都将不胜感激。非常感谢提前的帮助!

祝好!

Rapsong11

1个回答

14

这段代码片段应该可以完全实现您想要达到的目标

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

}

它只会通过软件包名称启动另一个应用程序

来源- 从自己的意图中打开另一个应用程序


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接