Android 安卓:点击小部件打开闹钟或时钟应用程序

Android 安卓:点击小部件打开闹钟或时钟应用程序,android,onclick,android-widget,clock,alarm,Android,Onclick,Android Widget,Clock,Alarm,不同的手机和供应商有不同的时钟应用程序。有代码可用于在某些手机上打开应用程序。不同的答案会给出不同的列表和策略,它们经常会出现工作问题。单击小部件打开时钟应用程序的可靠方法 此代码描述了如何通过单击小部件打开闹钟或桌面时钟应用程序。它通过其他解决方案解决了可靠性问题。它通过使用共享的首选项来提高电力经济性 查找手机应用程序的代码位于OneEnabled中。它找到应用程序,然后将信息保存在共享首选项中,由更新服务调用。一旦找到第一个包,它将停止搜索。如果要同时搜索闹钟和桌面时钟,请将首选选项放在阵

不同的手机和供应商有不同的时钟应用程序。有代码可用于在某些手机上打开应用程序。不同的答案会给出不同的列表和策略,它们经常会出现工作问题。

单击小部件打开时钟应用程序的可靠方法

此代码描述了如何通过单击小部件打开闹钟或桌面时钟应用程序。它通过其他解决方案解决了可靠性问题。它通过使用共享的首选项来提高电力经济性

查找手机应用程序的代码位于OneEnabled中。它找到应用程序,然后将信息保存在共享首选项中,由更新服务调用。一旦找到第一个包,它将停止搜索。如果要同时搜索闹钟和桌面时钟,请将首选选项放在阵列列表的较高位置。它不会在每次小部件更新时都执行此搜索,从而节省了电源。如果您的小部件经常更新,这将改变电池寿命

如果您觉得用户可能会更改其报警应用程序,则可以将OneEnabled代码放在WidgetProvider类中的单独方法中。然后从OneEnabled调用它,并在检测到用户时钟包中的更改时调用它

小部件提供程序类

公共类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
        }
    }
}
如果您有多个小部件提供程序,只需为每个提供程序复制从
//更新所有小部件实例
开始的代码,并更改小部件\u布局\u名称。此代码以相同的方式更新所有不同的小部件布局。如果您希望允许不同的小部件布局具有不同的首选项,您可以更改de来实现这一点——为每个小部件布局创建唯一标识符,并为每个布局创建一组共享首选项

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