How does Android start the system alarm clock in App

  • 2021-08-12 03:41:37
  • OfStack

Because the system alarm clock package names provided by different Android mobile phone manufacturers are different, we can only filter all the applications of the current mobile phone to determine the package name of the alarm clock application, and then start it.

The filtering rule used here is: system application & & Applied PackageName includes "widget" and "clock"

1. Layout file 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=" Start the system alarm clock " /> 
 
</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;// System installs all software  
  private List<PackageInfo> clockPackageInfos;// System clock software  
 
  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(), "-- Failed to start the system alarm clock 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(), "-- Failed to start the system alarm clock 2--", Toast.LENGTH_SHORT).show(); 
    } 
 
 
  } 
 
} 

Related articles: