【问题标题】:Widget with Image button that starts settings activity带有启动设置活动的图像按钮的小部件
【发布时间】:2014-01-14 15:28:23
【问题描述】:

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

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

代码sn-p:

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 方法吗?

【问题讨论】:

    标签: android button android-activity widget


    【解决方案1】:

    试试这个..

    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);
    

    【讨论】:

      【解决方案2】:

      开始考虑这个小部件和我的油炸...他帮我写了这段代码。

      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?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-07-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-02-22
        相关资源
        最近更新 更多