码迷,mamicode.com
首页 > 其他好文 > 详细

插件化-开启另外应用的activity

时间:2016-04-30 23:35:50      阅读:136      评论:0      收藏:0      [点我收藏+]

标签:

1、清单文件

android:sharedUserId="com.zyh.tplugin"

2、视图

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="vertical"
 6     android:id="@+id/ll"
 7     tools:context=".MainActivity" >
 8 
 9 
10 </LinearLayout>

3、java代码

 1 package com.itheimazyh.testplugin;
 2 
 3 import java.util.ArrayList;
 4 import java.util.HashMap;
 5 import java.util.List;
 6 import java.util.Map;
 7 
 8 import android.os.Bundle;
 9 import android.app.Activity;
10 import android.content.Intent;
11 import android.content.pm.PackageInfo;
12 import android.content.pm.PackageManager;
13 import android.graphics.Color;
14 import android.view.Menu;
15 import android.view.View;
16 import android.view.View.OnClickListener;
17 import android.widget.Button;
18 import android.widget.LinearLayout;
19 import android.widget.Toast;
20 
21 public class MainActivity extends Activity {
22     private LinearLayout ll;
23     @Override
24     protected void onCreate(Bundle savedInstanceState) {
25         super.onCreate(savedInstanceState);
26         setContentView(R.layout.activity_main);
27         
28         ll = (LinearLayout) findViewById(R.id.ll);
29         
30         List<Map<String, Object>> plugins = new ArrayList<Map<String, Object>>();
31         plugins = findPlugins();
32         if(plugins.size() > 0){
33             attachPlugin(plugins);
34         }else{
35             Toast.makeText(this, "没有相应的插件", 1).show();
36         }
37         
38     }
39     private void attachPlugin(List<Map<String, Object>> plugins) {
40         if(plugins.size() > 0){
41             for(Map<String, Object> map : plugins){
42                 Button button = new Button(this);
43                 button.setTextColor(Color.RED);
44                 String label = (String) map.get("label");
45                 final String pkgName = (String) map.get("pkgName");
46                 button.setText(label);
47                 
48                 ll.addView(button);
49                 button.setOnClickListener(new OnClickListener() {
50                     @Override
51                     public void onClick(View v) {
52                         Intent intent = new Intent();
53                         intent.setAction(pkgName);
54                         startActivity(intent);
55                     }
56                 });
57             }
58         }
59     }
60     private List<Map<String, Object>> findPlugins() {
61         List<Map<String, Object>> plugins = new ArrayList<Map<String, Object>>();
62         //包管理器
63         PackageManager pm = this.getPackageManager();
64         List<PackageInfo> installPackages = pm.getInstalledPackages(PackageManager.GET_UNINSTALLED_PACKAGES);//获取所有安装的package包
65         for(PackageInfo info : installPackages){
66             String pkgName = info.packageName;
67             String sharedUserId = info.sharedUserId;
68             if(sharedUserId == null || !sharedUserId.equals("com.zyh.tplugin") || pkgName.endsWith(this.getPackageName())){
69                 continue;
70             }
71             
72             String label = (String) pm.getApplicationLabel(info.applicationInfo);
73             
74             Map<String, Object> map = new HashMap<String, Object>();
75             map.put("label", label);
76             map.put("pkgName", pkgName);
77             plugins.add(map);
78         }
79         return plugins;
80     }
81 
82 }

 

插件化-开启另外应用的activity

标签:

原文地址:http://www.cnblogs.com/zhongyinghe/p/5449645.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!