不同的手机和供应商有不同的时钟应用程序。代码可用,打开一些手机的应用程序。不同的答案给出了不同的列表和策略,他们经常遇到问题。
答案 0 :(得分:1)
点击小部件时打开时钟应用程序的可靠方式。
此代码描述了如何通过单击窗口小部件打开警报或台钟应用程序。它解决了其他解决方案的可靠性问题。它通过使用共享首选项来增加电力经济性。
找到手机应用的代码放在onEnabled中。它找到应用程序然后将信息保存在共享首选项中,由更新服务调用。找到第一个包后,它就会停止搜索。如果要同时搜索警报和桌面时钟,请将首选选项放在数组列表的较高位置。每次更新小部件时都不会执行此搜索,从而节省了电量。如果您的小部件经常更新,这将对电池寿命产生影响。
如果您认为用户可能更改其警报应用程序,则可以将onEnabled代码放在WidgetProvider类中的单独方法中。然后从onEnabled调用它,并在检测到用户时钟包的更改时调用它。
WIDGET PROVIDER CLASS
public class WidgetProvider扩展AppWidgetProvider {
public static final String SHARED_PREFS = "SharedPrefs"; private PendingIntent pendingIntent = null; @Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { // start UpdateService whenever onUpdate is called Intent sIntent = new Intent(context, UpdateService.class); sIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startService(sIntent); } @Override public void onEnabled(Context context) { super.onEnabled(context); Log.w(LOG, "WidgetProvider.onEnabled triggered"); // find the local alarm service // this is an incomplete array of alarm+clock services, // in an arbitrary order of priority. // you may need to change the order to suit your requirements String clockImpls[][] = { { "Standard Alarm", "com.android.alarmclock", "com.android.alarmclock.AlarmClock" }, { "Sony Alarm", "com.sonyericsson.alarm", "com.sonyericsson.alarm.Alarm" }, { "Sony Ericsson Xperia Z", "com.sonyericsson.organizer", "com.sonyericsson.organizer.Organizer_WorldClock" }, { "ASUS Alarm Clock", "com.asus.alarmclock", "com.asus.alarmclock.AlarmClock" }, { "ASUS Desk Clock", "com.asus.deskclock", "com.asus.deskclock.DeskClock" }, { "HTC Alarm ClockDT", "com.htc.android.worldclock", "com.htc.android.worldclock.WorldClockTabControl" }, { "Standard Alarm ClockDT", "com.android.deskclock", "com.android.deskclock.AlarmClock" }, { "Froyo Nexus Alarm ClockDT", "com.google.android.deskclock", "com.android.deskclock.DeskClock" }, { "Moto Blur Alarm ClockDT", "com.motorola.blur.alarmclock", "com.motorola.blur.alarmclock.AlarmClock" }, { "Samsung Galaxy S", "com.sec.android.app.clockpackage", "com.sec.android.app.clockpackage.ClockPackage" } }; PackageManager packageManager = context.getPackageManager(); Intent alarmClockIntent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_LAUNCHER); boolean foundClockImpl = false; String vendorName = ""; String packageName = ""; String className = ""; for(int i=0; i<clockImpls.length; i++) { vendorName = clockImpls[i][0]; // not needed, for debugging only packageName = clockImpls[i][1]; className = clockImpls[i][2]; try { ComponentName cn = new ComponentName(packageName, className); packageManager.getActivityInfo(cn, PackageManager.GET_META_DATA); alarmClockIntent.setComponent(cn); foundClockImpl = true; } catch (PackageManager.NameNotFoundException e) { // Log.w(LOG, "AlarmService couldnt retrieve activity info"); } if (foundClockImpl) { // when the first package is found // send alarmCLockIntent to Shared Preferences // and break out of the for loop SharedPreferences settings = context.getSharedPreferences(SHARED_PREFS, 0); SharedPreferences.Editor editor = settings.edit(); editor.putString("VendorName", vendorName); // only needed for debugging editor.putString("PackageName", packageName); editor.putString("ClassName", className); // Commit the edits! editor.commit(); break; // stop searching to avoid setting less suitable options } } }
时钟应用程序阵列不完整,它包括一些常见的供应商。将有些设备没有这些应用程序。您可以将其他供应商的应用程序放在注释中,我会将它们编辑到数组中。我使用了应用程序&#34; System Tuner&#34;找到我的设备的包裹信息。
放置第一个小部件时,包信息已保存在“共享首选项”中。然后,每次更新窗口小部件时,UpdateService都会调用此信息以创建挂起的意图。建议:每次更新时,更新所有待处理的意图和任何其他远程视图更新。这样可以防止重启或更改方向等问题。
更新服务类
公共类UpdateService扩展了服务{
public static final String SHARED_PREFS = "SharedPrefs"; private PendingIntent pendingIntent = null; @Override public int onStartCommand(Intent intent, int flags, int startId) { // retrieve data from shared preferences SharedPreferences settings = getSharedPreferences(SHARED_PREFS, 0); // retrieve all your preferences each time, update everything int background = settings.getInt("Background", R.drawable.bg_box_light); // if cannot retrieve, set default to standard package // if it is not available, widget click will do nothing. String packageName = settings.getString("PackageName", "com.android.alarmclock"); String className = settings.getString("ClassName", "com.android.alarmclock.AlarmClock"); // get appwidgetmanager instance for all widgets AppWidgetManager localAppWidgetManager = AppWidgetManager.getInstance(this); // set up openAlarm PI PackageManager packageManager = this.getPackageManager(); Intent alarmClockIntent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_LAUNCHER); // construct pending intent from retrieved package info try { ComponentName cn = new ComponentName(packageName, className); packageManager.getActivityInfo(cn, PackageManager.GET_META_DATA); alarmClockIntent.setComponent(cn); } catch (PackageManager.NameNotFoundException e) { // Log or debug message } PendingIntent alarmPI = PendingIntent.getActivity(this, 0, alarmClockIntent, 0); // update all widget instances ComponentName thisWidget = new ComponentName(getBaseContext(), WidgetProvider.class); int[] allWidgetIds = localAppWidgetManager.getAppWidgetIds(thisWidget); for (int widgetId : allWidgetIds) { RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.widget_layout_name); // update EVERYTHING, EVERY TIME. // whatever is not updated may revert to the initial layout settings remoteViews.setImageViewResource(R.id.imageviewBG, background); remoteViews.setOnClickPendingIntent(R.id.analogClock, alarmPI); localAppWidgetManager.updateAppWidget(widgetId, remoteViews); }
如果您有多个窗口小部件提供程序,只需为每个提供程序复制从//update all widget instances
开始的代码,然后更改widget_layout_name。此代码以相同方式更新所有不同的窗口小部件布局。如果要允许不同的窗口小部件布局具有不同的首选项,则可以更改代码以实现此目的 - 为每个窗口小部件布局创建唯一标识符,并为每个布局设置一组共享首选项。