Question

please i need help, how can i make the text on my widget launch an activity to where the text (information) is located in the app or it should launch the activity where the text is located. Please kindly edit my code below, when answering. will gladly accept the answer that works.

public class WidgetUpdateService extends Service implements Handler.Callback{

String title;
String content;

 @Override
 public int onStartCommand(Intent intent, int flag, int startId) {
     handleCommand(intent);
     stopSelf();
    return START_STICKY;
 }

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void handleCommand(Intent intent) {
    AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this.getApplicationContext());

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

    for (int widgetId : allWidgetIds) {
          // create some random data
            Log.i("WIDGET SERVICE STARTED", "Fetching");
          RemoteViews remoteViews = new RemoteViews(this.getApplicationContext().getPackageName(), R.layout.cepf_appwidget_layout);
          remoteViews.setViewVisibility(R.id.widget_pb, View.VISIBLE);
          fetchFeed(remoteViews, widgetId, appWidgetManager);


          // Register an onClickListener
          Intent clickIntent = new Intent(this.getApplicationContext(), CepfWidgetProvider.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);
         //this is for the refresh button
          // remoteViews.setOnClickPendingIntent(R.id.sync_button, pendingIntent);
          //appWidgetManager.updateAppWidget(widgetId, remoteViews);
        }
}

private void fetchFeed(RemoteViews rm, int widgetId, AppWidgetManager mng){
    new Thread(new FetchTask(rm, widgetId, mng)).start();
}

class FetchTask implements Runnable{
    private RemoteViews rm;
    private int widgetId;
    private AppWidgetManager manager;
    public FetchTask(RemoteViews remoteView, int widgetId, AppWidgetManager manager){
        rm = remoteView;
        this.manager = manager;
        this.widgetId = widgetId;
    }

    @Override
    public void run() {
        String result=null;

        try{
            HttpClient httpclient=new DefaultHttpClient();
            HttpGet httppost=new HttpGet("");
            HttpResponse response=httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            result = EntityUtils.toString(entity);

            JSONObject json_data = new JSONObject(result);
            JSONArray query = json_data.getJSONArray("query");
            final JSONObject post = query.getJSONObject(0);

            Log.i("WIDGET SERVICE CONTENT", post.getString("content"));
            Log.i("WIDGET SERVICE TITLE", post.getString("title"));

            title = post.getString("title");
            content = post.getString("content");

            //Message msg = Message.obtain();
            //Bundle bundle = new Bundle();
            //bundle.putString("title", title);
            //bundle.putString("content", content);
            //msg.setData(bundle);
            rm.setTextViewText(R.id.title_widget, title);
            rm.setTextViewText(R.id.desc_widget, content);
            rm.setViewVisibility(R.id.widget_pb, View.GONE);
            manager.updateAppWidget(widgetId, rm);

        }catch(IOException ioe){
            Log.i("WIDGET FETCH FEED", ioe.getMessage());
        }
        catch(JSONException je){
            Log.i("WIDGET FETCH FEED", je.getMessage());
        }

    }

}

private Handler mHandler = new Handler(this);

@Override
public IBinder onBind(Intent arg0) {

    return null;
}

@Override
public boolean handleMessage(Message msg) {

    return false;
}

}

CepfWidgetProvider:

public class CepfWidgetProvider extends AppWidgetProvider {
 @Override
 public void onUpdate(Context context, AppWidgetManager appWidgetManager,
        int[] appWidgetIds) {
            ComponentName thisWidget = new ComponentName(context,
             CepfWidgetProvider.class);
            int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);

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

            // Update the widgets via the service
            context.startService(intent);
    }
}
Was it helpful?

Solution

Try this.

You could create this method within your CepfWidgetProvider class.

private PendingIntent openActivity(Context context)
{
    Intent intent = new Intent(Intent.ACTION_MAIN, null);
    intent.addCategory(Intent.CATEGORY_LAUNCHER); 
 // intent.putExtra(key, value); // just like you can do in startActivity(intent)

    // first param is app package name, second is package.class of the class you wish to open
    ComponentName cn = new ComponentName("com...", "com....ClassToOpenOnClick");
    intent.setComponent(cn);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    return PendingIntent.getActivity(context, 0, intent, 0);
}

Then you need to add a click listener to your textview in your widget

Edit: It has come to my attention you should try this in your CepfWidgetProvider.class first in the onUpdate() method.

// Uncomment the line below if you do not have remoteViews already in your onUpdate.
// RemoteViews remoteViews = new RemoteViews(context.getPackageName(),R.layout.yourWidgetLayout);

remoteViews.setOnClickPendingIntent(R.id.idOfYourWidgetTextView, openActivity(context));

Hope this works out for you! :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top