由于不同Android手机厂商提供的系统闹钟包名都不一致,所以只能通过过滤当前手机所有的应用,以确定闹钟应用的包名,进而启动它。
这里采用的过滤规则是:系统应用 && 应用的PackageName包含"widget" 和 "clock"
1、布局文件 activity_main.xml
2、MainActivity.java
package com.demo.cxc.systemclocksample; import android.content.Intent; import android.content.pm.ApplicationInfo; import android.content.pm.PackageInfo; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.Toast; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { private Button startSystemClockButton = null; private ListallPackageInfos;//系统安装所有软件 private List clockPackageInfos;//系统时钟软件 private static final int HANDLE_MESSAGE_KEY = 1001; private Handler myHandler = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); if (msg != null) { switch (msg.what) { case HANDLE_MESSAGE_KEY: Toast.makeText(getApplicationContext(), "--app scan over--", Toast.LENGTH_SHORT).show(); Log.d("CXC", "--app scan over--"); break; default: break; } } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initViews(); initData(); //begin scan installed applications; //// TODO: 16/1/15 new Thread(new ScanInstalledAppsRunnable()).start(); } private void initViews() { startSystemClockButton = (Button) findViewById(R.id.start_system_clock_bt); startSystemClockButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //// TODO: 16/1/15 startSystemClock(); } }); } private void initData() { clockPackageInfos = new ArrayList (); } class ScanInstalledAppsRunnable implements Runnable { @Override public void run() { allPackageInfos = getPackageManager() .getInstalledPackages(0); if (allPackageInfos == null || allPackageInfos.size() == 0) { //// TODO: 16/1/15 todo nothing return; } if (clockPackageInfos == null) { clockPackageInfos = new ArrayList (); } PackageInfo tempPackageInfo = null; for (int i = 0; i
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。