从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;
		}
	}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值