Android如何在App中启动系统闹钟

2020-11-01 15:04:10 浏览数 (1)

由于不同Android手机厂商提供的系统闹钟包名都不一致,所以只能通过过滤当前手机所有的应用,以确定闹钟应用的包名,进而启动它。

这里采用的过滤规则是:系统应用 && 应用的PackageName包含”widget” 和 “clock”

1、布局文件 activity_main.xml

代码语言:javascript复制
<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

代码语言:javascript复制
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(); 
} 
} 
} 

以上就是本文的全部内容,希望对大家的学习有所帮助。

0 人点赞