当应用程序被杀时,app widget click不起作用

时间:2015-09-28 15:08:23

标签: android widget mouseclick-event

我有一个小部件,当应用程序在后台运行时,它运行良好。但是当我用应用程序管理器杀死应用程序时,小部件不会触发点击事件。然后我重新打开应用程序,小部件仍然没有触发点击事件。有什么想法吗?

public class Widget extends AppWidgetProvider {

    private ArrayList<Widget> widgets;
    private static String listing1 = "";
    private static String listing2 = "";
    private static String listing3 = "";
    private static String listing4 = "";
    private static String listing5 = "";
    RemoteViews views;

    public void onUpdate(final Context context, final AppWidgetManager appWidgetManager, final int[] appWidgetIds) {
        Log.i("ExampleWidget", "Updating widgets " + Arrays.asList(appWidgetIds));

        // Perform this loop procedure for each App Widget that belongs to this

        HashMap<String, String> params = new HashMap<>();
        params.put("islem", "recommendation");

        ServiceConnector.secretcvAPI.getWidgetItems(params, new SuccessCallback<GetWidgetResponse>() {
            @Override
            public void success(GetWidgetResponse getWidgetResponse, Response response) {
                widgets = getWidgetResponse.widgets;

                for (int appWidgetId : appWidgetIds) {

                    listing1 = widgets.get(0).ilan_id;
                    listing2 = widgets.get(1).ilan_id;
                    listing3 =  widgets.get(2).ilan_id;
                    listing4 = widgets.get(3).ilan_id;
                    listing5 = widgets.get(4).ilan_id;
                    // Get the layout for the App Widget and attach an on-click listener
                    // to the button
                    views = new RemoteViews(context.getPackageName(), R.layout.widget);
                    views.setOnClickPendingIntent(R.id.listing1, getPendingSelfIntent(context, widgets.get(0).ilan_id, appWidgetId));
                    views.setOnClickPendingIntent(R.id.listing2, getPendingSelfIntent(context, widgets.get(1).ilan_id, appWidgetId));
                    views.setOnClickPendingIntent(R.id.listing3, getPendingSelfIntent(context, widgets.get(2).ilan_id, appWidgetId));
                    views.setOnClickPendingIntent(R.id.listing4, getPendingSelfIntent(context, widgets.get(3).ilan_id, appWidgetId));
                    views.setOnClickPendingIntent(R.id.listing5, getPendingSelfIntent(context, widgets.get(4).ilan_id, appWidgetId));

                    views.setTextViewText(R.id.listingNameTV, widgets.get(0).firma.isim);
                    views.setTextViewText(R.id.listingNameTV2, widgets.get(1).firma.isim);
                    views.setTextViewText(R.id.listingNameTV3, widgets.get(2).firma.isim);
                    views.setTextViewText(R.id.listingNameTV4, widgets.get(3).firma.isim);
                    views.setTextViewText(R.id.listingNameTV5, widgets.get(4).firma.isim);
                    views.setTextViewText(R.id.positionNameTV, widgets.get(0).ilan_baslik);
                    views.setTextViewText(R.id.positionNameTV2, widgets.get(1).ilan_baslik);
                    views.setTextViewText(R.id.positionNameTV3, widgets.get(2).ilan_baslik);
                    views.setTextViewText(R.id.positionNameTV4, widgets.get(3).ilan_baslik);
                    views.setTextViewText(R.id.positionNameTV5, widgets.get(4).ilan_baslik);

                    Picasso.with(context)
                            .load("http:" + widgets.get(0).firma.logo)
                            .into(views, R.id.widgetIV, new int[]{appWidgetId});

                    Picasso.with(context)
                            .load("http:" + widgets.get(1).firma.logo)
                            .into(views, R.id.widgetIV2, new int[]{appWidgetId});

                    Picasso.with(context)
                            .load("http:" + widgets.get(2).firma.logo)
                            .into(views, R.id.widgetIV3, new int[]{appWidgetId});

                    Picasso.with(context)
                            .load("http:" + widgets.get(3).firma.logo)
                            .into(views, R.id.widgetIV4, new int[]{appWidgetId});

                    Picasso.with(context)
                            .load("http:" + widgets.get(4).firma.logo)
                            .into(views, R.id.widgetIV5, new int[]{appWidgetId});

                    // Tell the AppWidgetManager to perform an update on the current app
                    // widget
                    appWidgetManager.updateAppWidget(appWidgetId, views);
                }

            }
        });
    }

    private PendingIntent getPendingSelfIntent(Context context, String action, int appWidgetId) {
        Intent intent = new Intent(context, getClass());
        intent.setAction(action);
        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);

        return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    }

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

        if (listing1.equals(intent.getAction())){
            startApplciationWithIntent(context, listing1);
        }

        else if (listing2.equals(intent.getAction())){
            startApplciationWithIntent(context, listing2);
        }

        else if (listing3.equals(intent.getAction())){
            startApplciationWithIntent(context, listing3);
        }

        else if (listing4.equals(intent.getAction())){
            startApplciationWithIntent(context, listing4);
        }

        else if (listing5.equals(intent.getAction())){
            startApplciationWithIntent(context, listing5);
        }
    }

    private void startApplciationWithIntent(Context context, String ilanid){
        Intent i = new Intent(context, LoggedOutActivity.class);
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        Bundle bundle = new Bundle();
        bundle.putString("DeepLink", ilanid);
        i.putExtras(bundle);
        context.startActivity(i);
    }
}

清单

<!-- Widget-->
    <receiver android:name=".Widget" android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
        </intent-filter>
        <meta-data android:name="android.appwidget.provider" android:resource="@xml/widget" />
    </receiver>

1 个答案:

答案 0 :(得分:0)

我目前正在寻找解决方案,希望这会有所帮助:

如果您查看soundcloud的小部件,您会注意到应用程序被杀,

  1. 小部件没有布局来表示“关闭”&#39;状态
  2. 点击它,它首先启动应用程序,更新小部件的布局和按钮
  3. 再次点击播放音乐。
  4. 所以,我想,你应该有一个服务运行,当用户点击小部件时,它将首先启动你的应用程序。