在获取的时候会有3秒左右的卡顿,因为Map插入上百条数据后再通过Ket遍历Value出来,所以会很慢
所以我就想在输入包名的时候就开始插入数据,简直就是(废物利用)节省时间,去除卡顿。
只要在创建多线程程序的时候,实现接口Callable,就可以获取线程中的返回值,而且方法是异步的。
Java
/*
* Create by JIULANG on 2020 9
*/
public class MainActivity extends Activity
{
private Button btn1,btn2 ;
private EditText et;
private LinkedHashMap<String,String> map= new LinkedHashMap<>();
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
et = findViewById(R.id.mainEditText1);
btn1 = findViewById(R.id.mainButton1);
btn1.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View p1)
{
String edit = et.getText().toString();
et.setText("");
long startTime = System.currentTimeMillis();
et.setHint(edit + " :" + getpackageName(edit));
long endTime = System.currentTimeMillis();
long usedTime = (endTime - startTime);
Toast.makeText(MainActivity.this, "耗时: " + usedTime + "", Toast.LENGTH_LONG).show();
}
});
//Thread 开一子线程
new Thread(new Runnable() {
public void run()
{
//获取HashMap数据
map = getAppInfoPuls();
}
}).start();
}
//通过程序名获取包名
private String getpackageName(String Name)
{
if (map != null)
{
//位置计数
int index = 0;
boolean hasContains = false;
//Map遍历
Object[] keys = map.keySet().toArray();
for (int i = 0; i < keys.length; i++)
{
String key = (String) keys[i];
if (key.equals(Name))
{
index = i;
hasContains = true;
break;
}
}
if (hasContains)
{
//返回包名
return (String)map.values().toArray()[index];
}
}
return "该应用不存在!";
}
public LinkedHashMap<String,String> getAppInfoPuls()
{
ExecutorService newCachedThreadPool = Executors.newCachedThreadPool();
//初始化HashMap
LinkedHashMap<String,String> returnValue = new LinkedHashMap<>();
GetPackageName get =new GetPackageName(this);
//从 Future 对象 获取任务返回值
Future<LinkedHashMap<String,String>> maps = newCachedThreadPool.submit(get);
while (true)
{
//可以用isDone()方法来查询Future是否已经完成,任务完成后,可以调用get()方法来获取结果
//注意: 如果不加判断直接调用get方法,此时如果线程未完成,get将阻塞,直至结果准备就绪
if (maps.isDone())
{
try
{
//开始计时
long startTime = System.currentTimeMillis();
/*
*获取返回值并写入
*/
returnValue = maps.get();
long endTime = System.currentTimeMillis();
long usedTime = (endTime - startTime);
//计算耗时
Log.d("耗时: " , usedTime + "ms");
//计算条数
Log.d("项目计数: " , returnValue.size() + "");
}
catch (Exception e){
e.printStackTrace();
}
//关闭线程池
newCachedThreadPool.shutdown();
//跳出循环
break;
}
}
//输出返回值
return returnValue;
}
}
多线程要实现Callable接口
构造函数好像不能用Activity,用了就不运行了,不知道为什么,请大佬指教
public class GetPackageName implements Callable<LinkedHashMap<String,String>>
{
private Context mContext;
private LinkedHashMap<String,String> map = new LinkedHashMap<>();
//构造函数好像不能用Activity,用了就不运行了,不知道为什么,请大佬指教
public GetPackageName(Context mContext){
this.mContext = mContext;
}
@Override
public LinkedHashMap<String, String> call() throws Exception
{
//LinkedHashMap
map = new LinkedHashMap<>();
//包管理器
PackageManager pm = mContext.getPackageManager();
List<PackageInfo> piList = pm.getInstalledPackages(PackageManager.GET_ACTIVITIES |
PackageManager.GET_CONFIGURATIONS |
PackageManager.GET_GIDS |
PackageManager.GET_INSTRUMENTATION |
PackageManager.GET_INTENT_FILTERS |
PackageManager.GET_META_DATA |
PackageManager.GET_PERMISSIONS |
PackageManager.GET_PROVIDERS |
PackageManager.GET_RECEIVERS |
PackageManager.GET_SERVICES |
PackageManager.GET_SHARED_LIBRARY_FILES |
PackageManager.GET_SIGNATURES |
PackageManager.GET_URI_PERMISSION_PATTERNS);
if (piList != null)
{
for (PackageInfo pi : piList)
{
//LinkedHashMap插入
map.put(pi.applicationInfo.loadLabel(pm).toString(),/*包名*/pi.packageName);
}
}
//返回处理好的LinkedHashMap值
return map;
}
XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<EditText
android:layout_width="wrap_content"
android:ems="10"
android:layout_height="wrap_content"
android:id="@+id/mainEditText1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="查询包名"
android:id="@+id/mainButton1"/>
</LinearLayout>
点个赞吧!