锁定屏幕小部件设置问题

时间:2013-02-01 06:38:26

标签: android

我正在开发一个在锁定屏幕小部件区域显示我的应用程序的应用程序,所以我找到了here的一些示例,我试图通过AppWidgetProvider类来处理服务,但是当我运行时它在模拟器上它不会在锁定屏幕上显示我的应用程序。 这是我的活动..

ExampleAppWidgetProvider asd = new ExampleAppWidgetProvider();
        AppWidgetManager appWidgetManager= AppWidgetManager.getInstance(getApplicationContext());
        int[] appWidgetIds = null;
        asd.onUpdate(getApplicationContext(), appWidgetManager,appWidgetIds);

ExampleAppWidgetProvider

private static final String LOG = "de.vogella.android.widget.example";

      @Override
      public void onUpdate(Context context, AppWidgetManager appWidgetManager,
          int[] appWidgetIds) {

        Log.w(LOG, "onUpdate method called");
        // Get all ids
        ComponentName thisWidget = new ComponentName(context,
                ExampleAppWidgetProvider.class);
        int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);

        // Build the intent to call the service
        Intent intent = new Intent(context.getApplicationContext(),
            UpdateWidgetService.class);
        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, allWidgetIds);

        // Update the widgets via the service
        context.startService(intent);
      }

UpdateWidgetService

public class UpdateWidgetService extends Service {
    private static final String LOG = "de.vogella.android.widget.example";

    @Override
    public void onStart(Intent intent, int startId) {
        Log.i(LOG, "Called");
        // Create some random data

        AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this
                .getApplicationContext());

        int[] allWidgetIds = intent
                .getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS);

        ComponentName thisWidget = new ComponentName(getApplicationContext(),
                ExampleAppWidgetProvider.class);
        int[] allWidgetIds2 = appWidgetManager.getAppWidgetIds(thisWidget);
        Log.w(LOG, "From Intent" + String.valueOf(allWidgetIds.length));
        Log.w(LOG, "Direct" + String.valueOf(allWidgetIds2.length));

        for (int widgetId : allWidgetIds) {
            // Create some random data
            int number = (new Random().nextInt(100));

            RemoteViews remoteViews = new RemoteViews(this
                    .getApplicationContext().getPackageName(),
                    R.layout.widget1);
            Log.w("WidgetExample", String.valueOf(number));
            // Set the text
            remoteViews.setTextViewText(R.id.app_name,
                    "Random: " + String.valueOf(number));

            // Register an onClickListener
            Intent clickIntent = new Intent(this.getApplicationContext(),
                    ExampleAppWidgetProvider.class);

            clickIntent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
            clickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS,
                    allWidgetIds);

            PendingIntent pendingIntent = PendingIntent.getBroadcast(
                    getApplicationContext(), 0, clickIntent,
                    PendingIntent.FLAG_UPDATE_CURRENT);
            remoteViews.setOnClickPendingIntent(R.id.movie_name, pendingIntent);
            appWidgetManager.updateAppWidget(widgetId, remoteViews);
        }
        stopSelf();

        super.onStart(intent, startId);
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}

1 个答案:

答案 0 :(得分:0)

根据我对类似问题的回答:

有两项更改可以使其作为锁定屏幕小部件使用:

  • 更新widgetCategory以包含keyguard
  • 添加initialKeyguardLayout

这些更改在./res/xml/widget_info.xml文件中完成,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
    android:initialKeyguardLayout="@layout/widget"
    android:initialLayout="@layout/widget"
    android:minHeight="40dp"
    android:minWidth="250dp"
    android:updatePeriodMillis="0"
    android:widgetCategory="home_screen|keyguard" >
</appwidget-provider>

看看其他答案 - 它包含一个非常简单的演示小部件的链接,您可以从我的github获取。他们可能会帮助你,即使他们与这个问题并不完全相关。