Android如何在App中启动系统闹钟
由于不同Android手机厂商提供的系统闹钟包名都不一致,所以只能通过过滤当前手机所有的应用,以确定闹钟应用的包名,进而启动它。
这里采用的过滤规则是:系统应用 && 应用的PackageName包含"widget" 和 "clock"
1、布局文件 activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <Button android:id="@+id/start_system_clock_bt" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="启动系统闹钟" /> </RelativeLayout>
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 List<PackageInfo> allPackageInfos;//系统安装所有软件 private List<PackageInfo> 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<PackageInfo>(); } 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>(); } PackageInfo tempPackageInfo = null; for (int i = 0; i < allPackageInfos.size(); i++) { tempPackageInfo = allPackageInfos.get(i); if (tempPackageInfo != null) { if (isSystemApplication(tempPackageInfo.applicationInfo) && isClockApplication(tempPackageInfo.packageName)) { clockPackageInfos.add(tempPackageInfo); } } } Message message = myHandler.obtainMessage(); message.what = MainActivity.HANDLE_MESSAGE_KEY; myHandler.sendMessage(message); } } private boolean isSystemApplication(ApplicationInfo applicationInfo) { boolean isSystemApp = false; if (applicationInfo != null) { if ((applicationInfo.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0 || (applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) { isSystemApp = true; } } return isSystemApp; } private boolean isClockApplication(String packageName) { boolean isClockApp = false; if (packageName != null && packageName.contains("clock") && !packageName.contains("widget")) { isClockApp = true; } return isClockApp; } private void startSystemClock() { if (clockPackageInfos == null || clockPackageInfos.size() == 0) { Toast.makeText(getApplicationContext(), "--启动系统闹钟失败1--", Toast.LENGTH_SHORT).show(); return; } Log.d("CXC", "---clock package size :" + clockPackageInfos.size()); Log.d("CXC", "---clock package [0] :" + clockPackageInfos.get(0).packageName); try { Intent startSysClockIntent = getPackageManager().getLaunchIntentForPackage(clockPackageInfos.get(0).packageName); startActivity(startSysClockIntent); } catch (Exception e) { Toast.makeText(getApplicationContext(), "--启动系统闹钟失败2--", Toast.LENGTH_SHORT).show(); } } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。
您可能感兴趣的文章:
- Android手机闹钟服务AlarmManagerk开发案例
- 简单实现Android闹钟程序 附源码
- Android闹钟设置的解决方案
- Android编程闹钟设置方法详解
- Android自定义View 实现闹钟唤起播放闹钟铃声功能
- 简单实现Android闹钟功能
- Android闹钟机制实现定时任务功能
- Android仿正点闹钟时间齿轮滑动效果
- Android使用CountDownTimer类实现倒计时闹钟
- android用闹钟定时做http请求推送的解决方案
赞 (0)