2013-09-21 43 views
3

嗨,我正在为教育Android启动,我需要它能够在用户点击了学校的工具按钮,它启动所安装的设备如何用按钮点击意图打开已安装的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

回答

8

这段代码应该做的正是你正在努力实现

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)

+0

谢谢非常 :) – rapsong11

相关问题