您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
这篇文章主要介绍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在App中启动系统闹钟的案例”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。