Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
266 views
in Technique[技术] by (71.8m points)

java - How to open a installed android app with a button click intent?

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

Here's the code

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);
    }

}

I am still new to android coding so please don't judge me

Any help would be amazing Thanks way in advance

Regards

Rapsong11

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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 - Open another application from your own (intent)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...