Android Widget Update服务,多个onclick监听器

时间:2014-04-23 08:07:30

标签: android android-intent android-widget android-service android-broadcast

我有以下小部件服务,当我点击我的一个按钮时,它会更新文本视图。

UpdateWidgetService:

ipublic class UpdateWidgetService extends Service {

private static final String LOG = "widgetService";
private static final String MyOnClick1 = "myOnClickTag1";
private static final String MyOnClick2 = "myOnClickTag2";
private static final String MyOnClick3 = "myOnClickTag3";

static LocationClient mLocationClient;

@Override
public void onStart(Intent intent, int startId) {

    Log.i(LOG, "Called and started");

    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(), R.layout.widget_layout);
        Log.w("WidgetExample", String.valueOf(number));
        // Set the text
        remoteViews.setTextViewText(R.id.widget_textview_gpscoords, "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(R.id.widget_button_dissarm, pendingIntent);
        appWidgetManager.updateAppWidget(widgetId, remoteViews);
    }
    stopSelf();
    super.onStart(intent, startId);
}

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

}

MyWidget Provider:

public class MyWidgetProvider extends AppWidgetProvider {

private static final String LOG = "de.vogella.android.widget.example";

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

android manifest:

<!-- Widget -->
    <receiver
        android:name="widget.MyWidgetProvider"
        android:icon="@drawable/icon"
        android:label="My Widget" >
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />                
        </intent-filter>

        <meta-data
            android:name="android.appwidget.provider"
            android:resource="@xml/example_appwidget_info" />
    </receiver>
    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />
    <!-- Services -->
    <service android:name="widget.UpdateWidgetService" >
    </service>

现在我的问题是,如何确定点击了哪个按钮?我的小部件上有4个按钮,我想区分哪些按钮被点击?在我的例子中,按钮onclick的意图是ACTION_APPWIDGET_UPDATE我想我需要改变一些如何?

这样做的正确方法是什么?

最终我的按钮都会成为网络连接,这就是我使用该服务的原因,因为网络通话可能需要一些时间。

1 个答案:

答案 0 :(得分:2)

我没有足够的声誉来评论你的帖子,所以我的答案只是为了从你那里获得更多信息,并向你展示如何为AppWidget按钮添加动作的简单示例。如果您接受我的回答,我会帮助您。顺便说一句,你没有在这里提供你的UpdateWidgetService.java,而是在AppWidgetProvider提供两次。

<强>更新

我会将UpdateWidgetService.java更改为以下代码。

public class UpdateWidgetService extends Service {

private static final String LOG = "widgetService";

// my changes - part 1 - beginning
private static final String BUTTON_ONE_CLICKED = "com.yourpackage.BUTTON_ONE_CLICKED";
private static final String BUTTON_TWO_CLICKED = "com.yourpackage.BUTTON_TWO_CLICKED";
private static final String BUTTON_THREE_CLICKED = "com.yourpackage.BUTTON_THREE_CLICKED";
private static final String BUTTON_FOUR_CLICKED = "com.yourpackage.BUTTON_FOUR_CLICKED";
// my changes - part 1 - end


@Override
public void onStart(Intent intent, int startId) {

    Log.i(LOG, "Called and started");

    // my changes - part 2 - beginning
    if(BUTTON_ONE_CLICKED.equals(intent.getAction())) {
        // do what should be done if button One has been clicked
    }
    if(BUTTON_TWO_CLICKED.equals(intent.getAction())) {
        // do what should be done if button Two has been clicked
    }
    if(BUTTON_THREE_CLICKED.equals(intent.getAction())) {
        // do what should be done if button Three has been clicked
    }
    if(BUTTON_FOUR_CLICKED.equals(intent.getAction())) {
        // do what should be done if button Four has been clicked
    }
    // my changes - part 2 - end

    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(), R.layout.widget_layout);
        Log.w("WidgetExample", String.valueOf(number));
        // Set the text
        remoteViews.setTextViewText(R.id.widget_textview_gpscoords, "Random: " + String.valueOf(number));

        // my changes - part 3 - beginning
        Context context = getApplicationContext();

        Intent buttonOneIntent = new Intent(context, UpdateWidgetService.class);
        Intent buttonTwoIntent = new Intent(context, UpdateWidgetService.class);
        Intent buttonThreeIntent = new Intent(context, UpdateWidgetService.class);
        Intent buttonFourIntent = new Intent(context, UpdateWidgetService.class);

        // set action
        buttonOneIntent.setAction(BUTTON_ONE_CLICKED);
        buttonTwoIntent.setAction(BUTTON_TWO_CLICKED);
        buttonThreeIntent.setAction(BUTTON_THREE_CLICKED);
        buttonFourIntent.setAction(BUTTON_FOUR_CLICKED);

        // put widgetId
        buttonOneIntent.putExtra("widgetId", widgetId);
        buttonTwoIntent.putExtra("widgetId", widgetId);
        buttonThreeIntent.putExtra("widgetId", widgetId);
        buttonFourIntent.putExtra("widgetId", widgetId);

        // make these intents unique to avoid collisions
        buttonOneIntent.setData(Uri.withAppendedPath(Uri.parse("webcall_widget://buttonone/widgetid"), String.valueOf(widgetId)));
        buttonTwoIntent.setData(Uri.withAppendedPath(Uri.parse("webcall_widget://buttontwo/widgetid"), String.valueOf(widgetId)));
        buttonThreeIntent.setData(Uri.withAppendedPath(Uri.parse("webcall_widget://buttonthree/widgetid"), String.valueOf(widgetId)));
        buttonFourIntent.setData(Uri.withAppendedPath(Uri.parse("webcall_widget://buttonfour/widgetid"), String.valueOf(widgetId)));

        // pending intents
        PendingIntent buttonOnePendingIntent = PendingIntent.getService(context, 0, buttonOneIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        PendingIntent buttonTwoPendingIntent = PendingIntent.getService(context, 0, buttonTwoIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        PendingIntent buttonThreePendingIntent = PendingIntent.getService(context, 0, buttonThreeIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        PendingIntent buttonFourPendingIntent = PendingIntent.getService(context, 0, buttonFourIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        // register onClickListeners to your buttons
        remoteViews.setOnClickPendingIntent(R.id.button_one, buttonOnePendingIntent);
        remoteViews.setOnClickPendingIntent(R.id.button_two, buttonTwoPendingIntent);
        remoteViews.setOnClickPendingIntent(R.id.button_three, buttonThreePendingIntent);
        remoteViews.setOnClickPendingIntent(R.id.button_four, buttonFourPendingIntent);
        // my changes - part 3 - end

        appWidgetManager.updateAppWidget(widgetId, remoteViews);
    }
    stopSelf();
    super.onStart(intent, startId);
}

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

如果不清楚,请不要犹豫,请问我。