android widget 点击进入应用

package com.ljapps.wifix.ui.provider;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.widget.RemoteViews;

import com.ljapps.wifix.R;
import com.ljapps.wifix.ui.activity.WifiXEntryActivity;

/**
 * Created by Alter on 16/9/7.
 */
public class WidgetIconProvider extends AppWidgetProvider {
    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        final int N = appWidgetIds.length;
        for (int i = 0; i < N; i++) {
            int appId = appWidgetIds[i];
            Intent intent = new Intent(context, WifiXEntryActivity.class);
            intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appId);  // Identifies the particular widget...
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

            intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
            PendingIntent pendIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

            RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.layout_widget);
            views.setOnClickPendingIntent(R.id.widget_icon, pendIntent);
            appWidgetManager.updateAppWidget(appId,views);
        }
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        super.onReceive(context, intent);
    }
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">
<ImageView
    android:id="@+id/widget_icon"
    android:layout_width="60dp"
    android:layout_height="60dp"
    android:background="@drawable/ic_launcher"/>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:initialLayout="@layout/layout_widget"
    android:minHeight="60dp"
    android:minWidth="60dp"
    android:resizeMode="horizontal|vertical"
    android:updatePeriodMillis="86400000"
    android:widgetCategory="home_screen|keyguard"></appwidget-provider>
<receiver android:name=".ui.provider.WidgetIconProvider">
            <meta-data android:name="android.appwidget.provider"
                android:resource="@layout/widget_icon_provider"></meta-data>
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
            </intent-filter>
        </receiver>

 

posted @ 2016-09-07 16:40  Alter  阅读(1144)  评论(0编辑  收藏  举报