Home Screen Widget step-by-step - Implement OnClick PendingIntent for Widget

It's part of the Home Screen Widgets step-by-step series.

We can cureat PendingIntent, and register it to widget's RemoteViews by setOnClickPendingIntent() method. As a result, when user click on the assigned view in the widgets, the PendingIntent will be trigged and to do something.

In the example, my blog (http://lesapplication.blogspot.com/) will be opened once user click on the ID in widgets.

Implement OnClick PendingIntent for Widget


Modify configure activity (ConfigureWidgetActivity.java) and App Widget Provider (WidgetProvider.java) to Prepare PendingIntent for remoteViews OnClickListener.

ConfigureWidgetActivity.java
package com.example.androidhomewidget;

import android.app.Activity;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RemoteViews;
import android.widget.TextView;

public class ConfigureWidgetActivity extends Activity {

int appWidgetId;
Button configureOkButton;
TextView wId;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.configure_layout);
wId = (TextView)findViewById(R.id.wid);
configureOkButton = (Button)findViewById(R.id.confighreok);
configureOkButton.setOnClickListener(configureOkButtonOnClickListener);

Intent intent = getIntent();
Bundle extras = intent.getExtras();

if (extras != null) {
appWidgetId = extras.getInt(
AppWidgetManager.EXTRA_APPWIDGET_ID,
AppWidgetManager.INVALID_APPWIDGET_ID);

wId.setText("appWidgetId = " + appWidgetId);

}else{
finish();
}

}

OnClickListener configureOkButtonOnClickListener
= new OnClickListener(){

@Override
public void onClick(View v) {

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

RemoteViews remoteViews = new RemoteViews(
getApplicationContext().getPackageName(),
R.layout.widget_layout);

remoteViews.setTextViewText(R.id.widget_id, String.valueOf(appWidgetId));
remoteViews.setTextViewText(R.id.widget_status, "Waiting...");

//--- Prepare PendingIntent for remoteViews OnClickListener
String myBlog = "http://lesapplication.blogspot.com/";
Uri Uri_myBlog = Uri.parse(myBlog);
Intent clickIntent = new Intent(Intent.ACTION_VIEW, Uri_myBlog);

int pendingRequestCode = 0;
int pendingFlag = 0;
PendingIntent pendingIntent
= PendingIntent.getActivity(
getApplicationContext(),
pendingRequestCode,
clickIntent,
pendingFlag);
remoteViews.setOnClickPendingIntent(R.id.widget_id, pendingIntent);
//--- End of Prepare PendingIntent

appWidgetManager.updateAppWidget(appWidgetId, remoteViews);

Intent intent = new Intent();
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
setResult(RESULT_OK, intent);
finish();

}};

}


WidgetProvider.java
package com.example.androidhomewidget;

import java.text.SimpleDateFormat;
import java.util.Date;

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 android.widget.Toast;

public class WidgetProvider extends AppWidgetProvider {

@Override
public void onDeleted(Context context, int[] appWidgetIds) {
// TODO Auto-generated method stub
super.onDeleted(context, appWidgetIds);
}

@Override
public void onDisabled(Context context) {
// TODO Auto-generated method stub
super.onDisabled(context);
}

@Override
public void onEnabled(Context context) {
// TODO Auto-generated method stub
super.onEnabled(context);
}

@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
super.onReceive(context, intent);
}

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

for(int i = 0; i < appWidgetIds.length; i++){

int id = appWidgetIds[i];

Toast.makeText(context,
"onUpdate: " + String.valueOf(id),
Toast.LENGTH_LONG).show();

RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);

remoteViews.setTextViewText(R.id.widget_id, String.valueOf(id));

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("hh:mm:ss");
String now = simpleDateFormat.format(new Date());
remoteViews.setTextViewText(R.id.widget_status, now);

//--- Prepare PendingIntent for remoteViews OnClickListener
String myBlog = "http://lesapplication.blogspot.com/";
Uri Uri_myBlog = Uri.parse(myBlog);
Intent clickIntent = new Intent(Intent.ACTION_VIEW, Uri_myBlog);

int pendingRequestCode = 0;
int pendingFlag = 0;
PendingIntent pendingIntent
= PendingIntent.getActivity(
context,
pendingRequestCode,
clickIntent,
pendingFlag);
remoteViews.setOnClickPendingIntent(R.id.widget_id, pendingIntent);
//--- End of Prepare PendingIntent

appWidgetManager.updateAppWidget(id, remoteViews);

super.onUpdate(context, appWidgetManager, appWidgetIds);
}
}

}



0 Response to "Home Screen Widget step-by-step - Implement OnClick PendingIntent for Widget"

Posting Komentar