0

在我的应用程序中,我有一个Fragment附加到 main 的activity. 这Fragment将在 的帮助下显示数据adapter。在 中adapter,我对一个layoutclickable text“在家观看”的设备进行了充气。adapter显示来自服务器的数据。我想在主屏幕上显示相同的数据,当我单击“从主屏幕观看”文本时,widget应该创建一个主屏幕,而无需任何用户交互。

我做了一个。“在家观看”receiver MyWidgetProvider的方法应该有什么才能将我带到小部件?onClickText()我必须承认,我是安卓新手。谢谢

Android 清单文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.pack.android.receiver"
    android:versionCode="1"
    android:versionName="1.0" >

    <application
        android:icon="@drawable/icon"
        android:label="@string/app_name" >
        <receiver android:name="MyWidgetProvider" >
            <intent-filter >
                <action 
                    android:name="android.appwidget.action.APPWIDGET_UPDATE" />
            </intent-filter>

            <meta-data
                android:name="android.appwidget.provider"
                android:resource="@xml/widget_info" />
        </receiver>
    </application>

    <uses-sdk android:minSdkVersion="8" />

</manifest> 

MyWidgetProvider.Java 文件

public class MyWidgetProvider extends AppWidgetProvider {

    private TextView team1Name;
    private TextView team2Name;
    private TextView team1Score;
    private TextView team2Score;

    public static boolean widgetView=false;
      private static final String LOG = "com.playup.android.receiver";

      public MyWidgetProvider(TextView team1Name, TextView team2Name, TextView team1Score, TextView team2Score){
          this.team1Name=team1Name;
          this.team2Name=team2Name;
          this.team1Score=team1Score;
          this.team2Score=team2Score;
          initializeViews();
      }

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

        Log.w(LOG, "onUpdate method called");
        // Get all ids
        ComponentName thisWidget = new ComponentName(context,MyWidgetProvider.class);
        int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);

        // Build the intent to call the service
        Intent intent = new Intent(context.getApplicationContext(), UpdateWidgetService.class);
        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, allWidgetIds);

        // Update the widgets via the service
        context.startService(intent);
      }

      public void initializeViews(){
    //    team1Name= (TextView)content_layout.findViewById(R.id.team1Name);

      }
} 

更新小部件服务

public class UpdateWidgetService extends Service {
  private static final String LOG = "com.playup.android.receiver";

  @Override
  public void onStart(Intent intent, int startId) {
    Log.i(LOG, "Called");
    // Create some random data

    AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this
        .getApplicationContext());

    int[] allWidgetIds = intent
        .getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS);

    ComponentName thisWidget = new ComponentName(getApplicationContext(),
        MyWidgetProvider.class);
    int[] allWidgetIds2 = appWidgetManager.getAppWidgetIds(thisWidget);
    Log.w(LOG, "From Intent" + String.valueOf(allWidgetIds.length));
    Log.w(LOG, "Direct" + String.valueOf(allWidgetIds2.length));

    for (int widgetId : allWidgetIds) {
      // Create some random data
      int number = (new Random().nextInt(100));

      RemoteViews remoteViews = new RemoteViews(this
          .getApplicationContext().getPackageName(),0x7f030061);        //Since R could not be resolve, I used the generated ids
      Log.w("Widget", String.valueOf(number));
      // Set the text
      remoteViews.setTextViewText(0x7f0a022a,"Random: " + String.valueOf(number));

      // Register an onClickListener
      Intent clickIntent = new Intent(this.getApplicationContext(),MyWidgetProvider.class);

      clickIntent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
      clickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS,
          allWidgetIds);

      PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, clickIntent,PendingIntent.FLAG_UPDATE_CURRENT);
      remoteViews.setOnClickPendingIntent(0x7f0a022a, pendingIntent);
      appWidgetManager.updateAppWidget(widgetId, remoteViews);
    }
    stopSelf();

    super.onStart(intent, startId);
  }

  @Override
  public IBinder onBind(Intent intent) {
    return null;
  }
4

3 回答 3

1

简单地说,你不能在没有任何用户交互的情况下创建一个家庭小部件。


更新:

你的接收者标签应该像这样开始:

<receiver
    android:name=".MyWidgetProvider" 
    android:label="My widget label" >
    <!-- The rest as is -->

</receiver>
于 2012-09-06T07:29:32.383 回答
1

主屏幕小部件只能由用户直接添加到主屏幕。只有用户可以将小部件作为访客添加到主屏幕或其他应用程序。

编辑:

要在小部件列表中查看您的小部件:

  1. 有必要AppWidgetProvider在清单中注册您的。
  2. 您的应用程序必须具有Launcher/Main活动,并且应用程序必须由用户直接运行,然后才能在小部件列表中可见。这是必要的,因为没有broadcastreceiver(所以没有 AppWidgetProvider),没有service,......您的应用程序无法在您之前注册应用程序直接由用户运行。如果您查看清单,您会看到您的应用程序没有Launcher/MainActivity(它只有一个接收器),因此它不能由用户运行,并且您的 AppWidgetProvider(即广播接收器)将不会注册和因此,您无法在小部件列表中看到您的小部件。
于 2012-09-06T07:30:32.407 回答
0

这让我摸不着头脑。我将meta-data标签作为标签的子intent-filter标签,这是错误的。meta-datareceiver标签的孩子。

版本不正确

 <receiver
            android:name=".AppWidgetReceiver"
            android:label="AppWidgetReceiver" >
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />

                <meta-data
                    android:name="android.appwidget.provider"
                    android:resource="@xml/appwidget_info" />
            </intent-filter>
        </receiver>

以下是正确版本

<receiver android:name=".AppWidgetReceiver"
            android:label="AppWidgetReceiver" >
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
            </intent-filter>

            <meta-data
                android:name="android.appwidget.provider"
                android:resource="@xml/appwidget_info" />
        </receiver>
于 2015-06-20T06:23:50.593 回答