新增:获取所有应用列表(方便复制APP包名)
parent
4b856f8d36
commit
2d4352d40f
@ -0,0 +1,157 @@
|
|||||||
|
package com.idormy.sms.forwarder;
|
||||||
|
|
||||||
|
import static com.idormy.sms.forwarder.SenderActivity.NOTIFY;
|
||||||
|
|
||||||
|
import android.annotation.SuppressLint;
|
||||||
|
import android.content.ClipData;
|
||||||
|
import android.content.ClipboardManager;
|
||||||
|
import android.content.Context;
|
||||||
|
import android.content.Intent;
|
||||||
|
import android.content.pm.ApplicationInfo;
|
||||||
|
import android.content.pm.PackageInfo;
|
||||||
|
import android.content.pm.PackageManager;
|
||||||
|
import android.graphics.drawable.Drawable;
|
||||||
|
import android.os.Bundle;
|
||||||
|
import android.os.Handler;
|
||||||
|
import android.os.Message;
|
||||||
|
import android.util.Log;
|
||||||
|
import android.view.View;
|
||||||
|
import android.widget.ListView;
|
||||||
|
import android.widget.RadioButton;
|
||||||
|
import android.widget.RadioGroup;
|
||||||
|
import android.widget.TextView;
|
||||||
|
import android.widget.Toast;
|
||||||
|
|
||||||
|
import androidx.appcompat.app.AppCompatActivity;
|
||||||
|
|
||||||
|
import com.idormy.sms.forwarder.adapter.AppAdapter;
|
||||||
|
import com.idormy.sms.forwarder.model.AppInfo;
|
||||||
|
import com.umeng.analytics.MobclickAgent;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@SuppressWarnings("deprecation")
|
||||||
|
public class AppListActivity extends AppCompatActivity {
|
||||||
|
|
||||||
|
public static final int APP_LIST = 0x9731991;
|
||||||
|
private final String TAG = "AppListActivity";
|
||||||
|
private List<AppInfo> appInfoList = new ArrayList<>();
|
||||||
|
private ListView listView;
|
||||||
|
private String currentType = "user";
|
||||||
|
|
||||||
|
//消息处理者,创建一个Handler的子类对象,目的是重写Handler的处理消息的方法(handleMessage())
|
||||||
|
@SuppressLint("HandlerLeak")
|
||||||
|
private final Handler handler = new Handler() {
|
||||||
|
@Override
|
||||||
|
public void handleMessage(Message msg) {
|
||||||
|
if (msg.what == NOTIFY) {
|
||||||
|
Toast.makeText(AppListActivity.this, msg.getData().getString("DATA"), Toast.LENGTH_LONG).show();
|
||||||
|
} else if (msg.what == APP_LIST) {
|
||||||
|
AppAdapter adapter = new AppAdapter(AppListActivity.this, R.layout.item_app, appInfoList);
|
||||||
|
listView.setAdapter(adapter);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
|
Log.d(TAG, "onCreate");
|
||||||
|
super.onCreate(savedInstanceState);
|
||||||
|
setContentView(R.layout.activity_applist);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onStart() {
|
||||||
|
super.onStart();
|
||||||
|
Log.d(TAG, "onStart");
|
||||||
|
Toast.makeText(AppListActivity.this, "加载应用列表中,请稍候...", Toast.LENGTH_LONG).show();
|
||||||
|
|
||||||
|
//是否关闭页面提示
|
||||||
|
TextView help_tip = findViewById(R.id.help_tip);
|
||||||
|
help_tip.setVisibility(MyApplication.showHelpTip ? View.VISIBLE : View.GONE);
|
||||||
|
|
||||||
|
//获取应用列表
|
||||||
|
getAppList();
|
||||||
|
|
||||||
|
//切换日志类别
|
||||||
|
int typeCheckId = "user".equals(currentType) ? R.id.btnTypeUser : R.id.btnTypeSys;
|
||||||
|
final RadioGroup radioGroupTypeCheck = findViewById(R.id.radioGroupTypeCheck);
|
||||||
|
radioGroupTypeCheck.check(typeCheckId);
|
||||||
|
radioGroupTypeCheck.setOnCheckedChangeListener((group, checkedId) -> {
|
||||||
|
RadioButton rb = findViewById(checkedId);
|
||||||
|
currentType = (String) rb.getTag();
|
||||||
|
getAppList();
|
||||||
|
});
|
||||||
|
|
||||||
|
listView = findViewById(R.id.list_view_app);
|
||||||
|
listView.setOnItemClickListener((parent, view, position, id) -> {
|
||||||
|
AppInfo appInfo = appInfoList.get(position);
|
||||||
|
Log.d(TAG, "onItemClick: " + appInfo.toString());
|
||||||
|
//复制到剪贴板
|
||||||
|
ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
|
||||||
|
ClipData mClipData = ClipData.newPlainText("pkgName", appInfo.getPkgName());
|
||||||
|
cm.setPrimaryClip(mClipData);
|
||||||
|
|
||||||
|
Toast.makeText(AppListActivity.this, "已复制包名:" + appInfo.getPkgName(), Toast.LENGTH_LONG).show();
|
||||||
|
});
|
||||||
|
listView.setOnItemLongClickListener((parent, view, position, id) -> {
|
||||||
|
AppInfo appInfo = appInfoList.get(position);
|
||||||
|
Log.d(TAG, "onItemClick: " + appInfo.toString());
|
||||||
|
//启动应用
|
||||||
|
Intent intent;
|
||||||
|
intent = getPackageManager().getLaunchIntentForPackage(appInfo.getPkgName());
|
||||||
|
startActivity(intent);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
//获取应用列表
|
||||||
|
private void getAppList() {
|
||||||
|
new Thread(() -> {
|
||||||
|
appInfoList = new ArrayList<>();
|
||||||
|
PackageManager pm = getApplication().getPackageManager();
|
||||||
|
@SuppressLint("QueryPermissionsNeeded") List<PackageInfo> packages = pm.getInstalledPackages(PackageManager.GET_UNINSTALLED_PACKAGES);
|
||||||
|
for (PackageInfo packageInfo : packages) {
|
||||||
|
if ("user".equals(currentType) && (packageInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 1) { //用户应用
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if ("sys".equals(currentType) && (packageInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) { //系统应用
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
String appName = packageInfo.applicationInfo.loadLabel(pm).toString();
|
||||||
|
String packageName = packageInfo.packageName;
|
||||||
|
Drawable drawable = packageInfo.applicationInfo.loadIcon(pm);
|
||||||
|
String verName = packageInfo.versionName;
|
||||||
|
int verCode = packageInfo.versionCode;
|
||||||
|
AppInfo appInfo = new AppInfo(appName, packageName, drawable, verName, verCode);
|
||||||
|
appInfoList.add(appInfo);
|
||||||
|
Log.d(TAG, appInfo.toString());
|
||||||
|
}
|
||||||
|
Message message = new Message();
|
||||||
|
message.what = APP_LIST;
|
||||||
|
message.obj = appInfoList;
|
||||||
|
handler.sendMessage(message);
|
||||||
|
}).start();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onDestroy() {
|
||||||
|
Log.d(TAG, "onDestroy");
|
||||||
|
super.onDestroy();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onResume() {
|
||||||
|
super.onResume();
|
||||||
|
MobclickAgent.onResume(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onPause() {
|
||||||
|
super.onPause();
|
||||||
|
MobclickAgent.onPause(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,47 @@
|
|||||||
|
package com.idormy.sms.forwarder.model;
|
||||||
|
|
||||||
|
import android.content.Intent;
|
||||||
|
import android.graphics.drawable.Drawable;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public class AppInfo {
|
||||||
|
public String pkgName;
|
||||||
|
public String appName;
|
||||||
|
public Drawable appIcon;
|
||||||
|
public Intent appIntent;
|
||||||
|
public String verName;
|
||||||
|
public int verCode;
|
||||||
|
|
||||||
|
public AppInfo() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public AppInfo(String appName) {
|
||||||
|
this.appName = appName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public AppInfo(String appName, String pkgName) {
|
||||||
|
this.appName = appName;
|
||||||
|
this.pkgName = pkgName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public AppInfo(String appName, String pkgName, Drawable appIcon, String verName, int verCode) {
|
||||||
|
this.appName = appName;
|
||||||
|
this.pkgName = pkgName;
|
||||||
|
this.appIcon = appIcon;
|
||||||
|
this.verName = verName;
|
||||||
|
this.verCode = verCode;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "AppInfo{" +
|
||||||
|
"appName='" + appName + '\'' +
|
||||||
|
", pkgName='" + pkgName + '\'' +
|
||||||
|
", appIcon=" + appIcon +
|
||||||
|
", verName=" + verName +
|
||||||
|
", verCode=" + verCode +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,51 @@
|
|||||||
|
<?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:orientation="vertical">
|
||||||
|
|
||||||
|
<LinearLayout
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_gravity="center_horizontal"
|
||||||
|
android:layout_marginTop="5dip"
|
||||||
|
android:orientation="horizontal">
|
||||||
|
|
||||||
|
<RadioGroup
|
||||||
|
android:id="@+id/radioGroupTypeCheck"
|
||||||
|
style="@style/rg_style"
|
||||||
|
android:orientation="horizontal">
|
||||||
|
|
||||||
|
<RadioButton
|
||||||
|
android:id="@+id/btnTypeUser"
|
||||||
|
style="@style/select_style"
|
||||||
|
android:tag="user"
|
||||||
|
android:text="@string/user_app"
|
||||||
|
android:checked="true" />
|
||||||
|
|
||||||
|
<RadioButton
|
||||||
|
android:id="@+id/btnTypeSys"
|
||||||
|
style="@style/select_style"
|
||||||
|
android:tag="sys"
|
||||||
|
android:text="@string/system_app" />
|
||||||
|
|
||||||
|
</RadioGroup>
|
||||||
|
|
||||||
|
</LinearLayout>
|
||||||
|
|
||||||
|
<ListView
|
||||||
|
android:id="@+id/list_view_app"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="0dp"
|
||||||
|
android:layout_margin="5dp"
|
||||||
|
android:layout_weight="1" />
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:id="@+id/help_tip"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:gravity="center"
|
||||||
|
android:text="@string/app_tips"
|
||||||
|
android:textColor="@color/colorPrimary" />
|
||||||
|
|
||||||
|
</LinearLayout>
|
Loading…
Reference in New Issue