android 调用本地第三方应用软件,如qq、微信、微博和视频播放器等
生活随笔
收集整理的這篇文章主要介紹了
android 调用本地第三方应用软件,如qq、微信、微博和视频播放器等
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
在做Android開發(fā)的過程中有很多時候要調(diào)用第三方的軟件來輔助自己的軟件完成相應(yīng)的功能,比如在一個軟件中調(diào)用QQ或者微信來進行聊天,或者是調(diào)用第三方的視頻軟件來視頻播放等。。。經(jīng)過查找資料有很多資料是通過
調(diào)用第三方應(yīng)用的兩個參數(shù),應(yīng)用的包名和類名才能調(diào)用打開第三方程序。例如下面這樣
ComponentName componentName = new ComponentName(pkg, cls);
Intent intent = new Intent();
intent.setComponent(componentName);
startActivity(intent);
直接設(shè)置pkg和cls這樣也可以打開第三方應(yīng)用,首先你要知道你要調(diào)用的軟件的包名和類名,不知道的話就根本沒辦法,我在開發(fā)過程中需要調(diào)用QQ來進行相互之間的聯(lián)系,在其中一個手機上是可以運行的,但是將這個軟件安裝在另一個手機上的時候打開過程中就會出錯,經(jīng)過一段時間的查找問題才發(fā)現(xiàn)是QQ版本上的問題,其中的一個版本很老,包名和類名都跟現(xiàn)在的不一樣。
所以在這里我推薦用下面的方法調(diào)用第三方應(yīng)用,通過查找手機內(nèi)所有軟件的包名和類名,將其保存,然后通過查找匹配包名中的關(guān)鍵字調(diào)用第三方軟件。
得到包名后就可以根據(jù)包名來調(diào)用第三方軟件了。下面進入正題:
布局文件,兩個按鍵,一個打開QQ和一個打開微信:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" ><Button android:id="@+id/qqButton"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="打開QQ"/><Button android:id="@+id/weixinButton"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="打開微信"/></LinearLayout>實現(xiàn)類
MainActivity.java
package cn.edu.cqu.openqqweixin;import java.util.Collections; import java.util.List; import java.util.Map;import android.app.Activity; import android.content.ComponentName; import android.content.Intent; import android.content.pm.PackageManager; import android.content.pm.ResolveInfo; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button;public class MainActivity extends Activity {private Button qqButton;private Button weixinButton;private PackageManager mPackageManager;private List<ResolveInfo> mAllApps;private String input;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);qqButton = (Button) findViewById(R.id.qqButton);weixinButton = (Button) findViewById(R.id.weixinButton);qqButton.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubinput = "qq";openApp(input);}});weixinButton.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubinput = "mm";openApp(input);}});}private void openApp(String str){//應(yīng)用過濾條件Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);System.out.println("testrrr");mPackageManager = this.getPackageManager();System.out.println("tesr");mAllApps = mPackageManager.queryIntentActivities(mainIntent, 0);//按報名排序Collections.sort(mAllApps, new ResolveInfo.DisplayNameComparator(mPackageManager));for(ResolveInfo res : mAllApps){//該應(yīng)用的包名和主ActivityString pkg = res.activityInfo.packageName;String cls = res.activityInfo.name;System.out.println("pkg---" +pkg);// System.out.println("打印出來的----" + str);// 打開QQ pkg中包含"qq",打開微信,pkg中包含"mm"if(pkg.contains(str)){ComponentName componet = new ComponentName(pkg, cls);Intent intent = new Intent();intent.setComponent(componet);intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);this.startActivity(intent);}}} } 程序中設(shè)置了一個 String類型input,通過設(shè)置input可以打開不同的應(yīng)用程序。
總結(jié)
以上是生活随笔為你收集整理的android 调用本地第三方应用软件,如qq、微信、微博和视频播放器等的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2根4平方的电线能拧在一起,当6平方的电
- 下一篇: android 一个activity定时