android通过包名跳转到系统闹钟

时间:2021-06-30 16:45:03

主要是通过包名来跳转系统的闹钟



这个在部分手机不可以跳转,

  //跳转系统闹钟
// Intent alarmas = new Intent(AlarmClock.ACTION_SET_ALARM);
// startActivity(alarmas);

所以要使用通过包名来调用系统闹钟

public class SystemClockDemoActivity extends Activity {
Map<String, Object> item;
private ArrayList<String> pagList;
private Button button;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button = (Button) findViewById(R.id.button);

pagList = new ArrayList<String>();

button.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
listPackages();
Log.d("mxt", "paglist的大小:" + pagList.size());
for (int i = 0; i < pagList.size(); i++) {
Log.d("mxt", pagList.get(i));
}

PackageManager pm = getPackageManager();

Intent i = pm.getLaunchIntentForPackage(pagList.get(0));
// 如果该程序不可启动(像系统自带的包,有很多是没有入口的)会返回NULL
if (i != null) {
SystemClockDemoActivity.this.startActivity(i);
} else {
Intent i2 = new Intent(Settings.ACTION_DATE_SETTINGS);
SystemClockDemoActivity.this.startActivity(i);
}

}
});

}

class PInfo {
private String appname = "";
private String pname = "";
private String versionName = "";
private int versionCode = 0;
private Drawable icon;

private void prettyPrint() {
Log.i("taskmanger", appname + "\t" + pname + "\t" + versionName
+ "\t" + versionCode + "\t");
}
}

private void listPackages() {
ArrayList<PInfo> apps = getInstalledApps(false); /*
* false = no system
* packages
*/
final int max = apps.size();
for (int i = 0; i < max; i++) {
apps.get(i).prettyPrint();
item = new HashMap<String, Object>();

int aa = apps.get(i).pname.length();
if (aa > 11) {
if (apps.get(i).pname.indexOf("clock") != -1) {
if (!(apps.get(i).pname.indexOf("widget") != -1)) {
try {
PackageInfo pInfo = getPackageManager()
.getPackageInfo(apps.get(i).pname, 0);
if (isSystemApp(pInfo) || isSystemUpdateApp(pInfo)) {
Log.d("mxt", "是系统自带的");
Log.d("mxt",
"找到了"
+ apps.get(i).pname
.substring(apps.get(i).pname
.length() - 5)
+ " 全名:" + apps.get(i).pname
+ " " + apps.get(i).appname);
item.put("pname", apps.get(i).pname);
item.put("appname", apps.get(i).appname);
pagList.add(apps.get(i).pname);
}
} catch (Exception e) {
// TODO: handle exception
}

}
}
}

}
}

private ArrayList<PInfo> getInstalledApps(boolean getSysPackages) {
ArrayList<PInfo> res = new ArrayList<PInfo>();
List<PackageInfo> packs = getPackageManager().getInstalledPackages(0);
for (int i = 0; i < packs.size(); i++) {
PackageInfo p = packs.get(i);
if ((!getSysPackages) && (p.versionName == null)) {
continue;
}
PInfo newInfo = new PInfo();
newInfo.appname = p.applicationInfo.loadLabel(getPackageManager())
.toString();
newInfo.pname = p.packageName;
newInfo.versionName = p.versionName;
newInfo.versionCode = p.versionCode;
newInfo.icon = p.applicationInfo.loadIcon(getPackageManager());
res.add(newInfo);
}
return res;
}

public boolean isSystemApp(PackageInfo pInfo) {
return ((pInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0);
}

public boolean isSystemUpdateApp(PackageInfo pInfo) {
return ((pInfo.applicationInfo.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0);
}
}