带有图像按钮的小组件,用于启动设置活动

时间:2014-01-14 15:28:24

标签: android button android-activity widget

我正在尝试使用1 button创建一个简单的小部件。按此button将启动android设置活动(例如android.provider.Settings.ACTION_WIFI_SETTINGS)。

我的代码有什么问题?我该怎么做才能让它发挥作用?

代码段:

public class WiFiShortcut extends AppWidgetProvider{

//ImageButton imagebutton;
static RemoteViews remoteViews;
public static String ACTION="SetWIFI";

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

    remoteViews = new RemoteViews( context.getPackageName(), R.layout.wifi_shortcut_main );
    Intent cel= new Intent(android.provider.Settings.ACTION_WIFI_SETTINGS);
    //Intent cel = new Intent(context, WiFiShortcut.class);
    cel.setAction(ACTION);
    PendingIntent pcel = PendingIntent.getBroadcast(context,0, cel,0);
    remoteViews.setOnClickPendingIntent(R.id.bwifi, pcel);
}

}

我应该使用onReceive方法吗?

2 个答案:

答案 0 :(得分:0)

试试这个..

Intent cel= new Intent(android.provider.Settings.ACTION_WIFI_SETTINGS);

PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, cel, 0);

ResolveInfo resolveInfo1 = context.getPackageManager().resolveActivity(cel, 0);
if(resolveInfo1 != null)
        remoteViews.setOnClickPendingIntent(R.id.bwifi, pendingIntent);

答案 1 :(得分:0)

开始用我的炒作思考这个小部件......他帮我写了这段代码。

package com.mbaryla.wifisettings;

import java.util.Random;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.net.wifi.WifiManager;
import android.util.Log;
import android.widget.RemoteViews;

public class WiFiShortcut extends AppWidgetProvider {

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

    ComponentName thisWidget = new ComponentName(context, WiFiShortcut.class);

    int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
    for (int widgetId : allWidgetIds) {

        RemoteViews remoteViews = new RemoteViews(context.getPackageName(),R.layout.main);
        Log.w("WidgetExample", String.valueOf(number));

        Intent intent = new Intent(WifiManager.ACTION_PICK_WIFI_NETWORK);

        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
        remoteViews.setOnClickPendingIntent(R.id.txt, pendingIntent);
        appWidgetManager.updateAppWidget(widgetId, remoteViews);
    }
}

}

它有效,但只有当他编译这段代码并开始在我的手机上。 当我在我的计算机上编译并将其拖到手机上时,它无法正常工作......

这怎么可能???????? 可能是因为Manifest?