Android小部件与小部件

时间:2017-03-09 13:57:14

标签: java android widget

我有一个Android应用,其中service在应用启动时启动,当应用被杀时,service也会被销毁。 我有一个home screen widget与app一起开始。它有3个图像视图 - "播放","下一个","上一个"。 在按下窗口小部件中的任何imageview时,他们需要与serviceservice与图书馆等进行对话。 当service有答案时,它会响应状态为OK或NO的窗口小部件。根据回复,我需要更新imageviews

当应用程序被杀死时,我需要完全灰化我的小部件。在这种情况下,当我点击灰色的小部件时,我需要启动应用程序。有人可以帮我吗 我怎么能做到这一点。

这是我的小部件代码 -

 public class MyWidgetProvider extends AppWidgetProvider {

    private Intent serviceIntent;

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

        RemoteViews remoteViews;
        ComponentName componentName;

        remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
        componentName = new ComponentName(context, ControlScreenWidgetProvider.class);

        remoteViews.setOnClickPendingIntent(R.id.PLAY,getPendingSelfIntent(context, "PLAY",componentName));
        remoteViews.setOnClickPendingIntent(R.id.NEXT,getPendingSelfIntent(context, "NEXT",componentName));
        remoteViews.setOnClickPendingIntent(R.id.PREVIOUS,getPendingSelfIntent(context, "PREVIOUS",componentName));

        appWidgetManager.updateAppWidget(componentName, remoteViews);
    }

    @Override
    public void onReceive(Context context, Intent intent)
    {
        super.onReceive(context, intent);
        AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);

        RemoteViews remoteViews;
        ComponentName componentName;

        remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
        componentName = new ComponentName(context, MyWidgetProvider.class);

        if ("PLAY".equals(intent.getAction())) {

            remoteViews.setOnClickPendingIntent(R.id.PLAY,getPendingSelfIntent(context, "PLAY",componentName));
            // Dont know what to do here

        }
        else if ("NEXT".equals(intent.getAction())) {

            remoteViews.setOnClickPendingIntent(R.id.lock_door,getPendingSelfIntent(context, "NEXT",componentName));
            // Dont know what to do here

        }
        else if ("PREVIOUS".equals(intent.getAction())) {

            remoteViews.setOnClickPendingIntent(R.id.lock_door,getPendingSelfIntent(context, "PREVIOUS",componentName));
            // Dont know what to do here

        }
        appWidgetManager.updateAppWidget(componentName, remoteViews);
    }

    protected PendingIntent getPendingSelfIntent(Context context, String action, ComponentName componentName) {
        Intent intent = new Intent(context, MyWidgetProvider.class);
        intent.setAction(action);
        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, componentName);
        return PendingIntent.getService(context, 0, intent, 0);
    }
}

0 个答案:

没有答案