从appWidget开启系统自带AlarmClock

Since: API Level 9  有了统一的方法:

		if(Build.VERSION.SDK_INT >=9){
			Intent intentClock= new Intent();
			intentClock.setAction(AlarmClock.ACTION_SET_ALARM);
			return PendingIntent.getActivity(context, 0, intentClock, 0);
		}

此时注意添加权限:com.android.alarm.permission.SET_ALARM


之前的就比较混乱:

	        PackageManager packageManager = context.getPackageManager();
		Intent alarmClockIntent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_LAUNCHER);
		// Verify clock implementation
		String clockImpls[][] = {
				{"HTC Alarm Clock", "com.htc.android.worldclock", "com.htc.android.worldclock.WorldClockTabControl" },
				{"Standar Alarm Clock", "com.android.deskclock", "com.android.deskclock.AlarmClock"},
				{"Froyo Nexus Alarm Clock", "com.google.android.deskclock", "com.android.deskclock.DeskClock"},
				{"Moto Blur Alarm Clock", "com.motorola.blur.alarmclock",  "com.motorola.blur.alarmclock.AlarmClock"},
				{"Samsung Galaxy Clock", "com.sec.android.app.clockpackage","com.sec.android.app.clockpackage.ClockPackage"},
				{"google 2.1 Clock", "com.android.deskclock","com.android.deskclock.DeskClock"},
				{"emulator 2.1 Clock", "com.android.alarmclock","com.android.alarmclock.AlarmClock"}
		};

		boolean foundClockImpl = false;

		for(int i=0; i<clockImpls.length; i++) {
			String vendor = clockImpls[i][0];
			String packageName = clockImpls[i][1];
			String className = clockImpls[i][2];
			try {
				ComponentName cn = new ComponentName(packageName, className);
				ActivityInfo aInfo = packageManager.getActivityInfo(cn, PackageManager.GET_META_DATA);
				alarmClockIntent.setComponent(cn);
				Log.v(TAG,"Found " + vendor + " --> " + packageName + "/" + className);
				foundClockImpl = true;
			} catch (NameNotFoundException e) {
				Log.v(TAG,vendor + " does not exists");
			}
		}

		if (foundClockImpl) {
			PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, alarmClockIntent, 0);
			return pendingIntent;
		}else{
			return null;
		}
部分来自网络,没有一一验证。


完整函数:

	// 获取闹钟的PendingIntent
	private PendingIntent getClockPendingIntent() {

		if(Build.VERSION.SDK_INT >=9){
			Intent intentClock= new Intent();
			intentClock.setAction(AlarmClock.ACTION_SET_ALARM);
			return PendingIntent.getActivity(context, 0, intentClock, 0);
		}
		Log.v(TAG,"SDK_INT:" + Build.VERSION.SDK_INT);
		
		PackageManager packageManager = context.getPackageManager();
		Intent alarmClockIntent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_LAUNCHER);
		// Verify clock implementation
		String clockImpls[][] = {
				{"HTC Alarm Clock", "com.htc.android.worldclock", "com.htc.android.worldclock.WorldClockTabControl" },
				{"Standar Alarm Clock", "com.android.deskclock", "com.android.deskclock.AlarmClock"},
				{"Froyo Nexus Alarm Clock", "com.google.android.deskclock", "com.android.deskclock.DeskClock"},
				{"Moto Blur Alarm Clock", "com.motorola.blur.alarmclock",  "com.motorola.blur.alarmclock.AlarmClock"},
				{"Samsung Galaxy Clock", "com.sec.android.app.clockpackage","com.sec.android.app.clockpackage.ClockPackage"},
				{"google 2.1 Clock", "com.android.deskclock","com.android.deskclock.DeskClock"},
				{"emulator 2.1 Clock", "com.android.alarmclock","com.android.alarmclock.AlarmClock"}
		};

		boolean foundClockImpl = false;

		for(int i=0; i<clockImpls.length; i++) {
			String vendor = clockImpls[i][0];
			String packageName = clockImpls[i][1];
			String className = clockImpls[i][2];
			try {
				ComponentName cn = new ComponentName(packageName, className);
				ActivityInfo aInfo = packageManager.getActivityInfo(cn, PackageManager.GET_META_DATA);
				alarmClockIntent.setComponent(cn);
				Log.v(TAG,"Found " + vendor + " --> " + packageName + "/" + className);
				foundClockImpl = true;
			} catch (NameNotFoundException e) {
				Log.v(TAG,vendor + " does not exists");
			}
		}

		if (foundClockImpl) {
			PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, alarmClockIntent, 0);
			return pendingIntent;
		}else{
			return null;
		}
	}



你可能感兴趣的:(从appWidget开启系统自带AlarmClock)