Krishna Gupta
Krishna Gupta

Reputation: 1309

Logical Flow of all Intents (PendingIntents) in this app widget snippet

I am learning how to build home screen widget in Android. This code is for an app widget to toggle RingerMode from NORMAL to SILENT and vice-versa.

This works fine but I need a full in-sight of logical flow (i.e which gets initiated when, goes where, does what, dies when) of all the intents in this.

Please help me understand this topic clearer.

package com.dummies.android.silentmodetoggle;

import android.app.Activity;
import android.app.IntentService;
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.media.AudioManager;
import android.util.Log;
import android.widget.RemoteViews;

public class AppWidget extends AppWidgetProvider {

public static String tag ="SilentModeToggleWidget";

@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub
    Log.d(tag, "onReceive() first line");


    if(intent.getAction()==null)
    {
        //Do Something
        Log.d(tag, "before startService()");
        context.startService(new Intent(context, ToggleService.class));
    }
    else{
        super.onReceive(context, intent);
    }



}


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

    // Do something in specified intervals
    context.startService(new Intent(context, ToggleService.class));
}

public static class ToggleService extends IntentService{

    public ToggleService(){

        super("AppWidget$ToggleService");

        Log.d(tag, "In ToggleService construcor");
    }

    @Override
    protected void onHandleIntent(Intent arg0) {

        Log.d(tag, "In ToggleService > onHandleIntent");

        // TODO Auto-generated method stub
        ComponentName cn = new ComponentName(this, AppWidget.class);
        AppWidgetManager mgr = AppWidgetManager.getInstance(this);

        mgr.updateAppWidget(cn, buildUpdate(this));

    }


    private RemoteViews buildUpdate(Context context){
        RemoteViews updateViews = new RemoteViews(context.getPackageName(), R.layout.widget);

        AudioManager audioManager = (AudioManager)context.getSystemService(Activity.AUDIO_SERVICE);

        if(audioManager.getRingerMode() == AudioManager.RINGER_MODE_SILENT){
            updateViews.setImageViewResource(R.id.phoneState, R.drawable.phone_on);
            audioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
        }

        else{
            updateViews.setImageViewResource(R.id.phoneState, R.drawable.phone_silent);
            audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
        }


        Intent i = new Intent(this, AppWidget.class);

        PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);

        updateViews.setOnClickPendingIntent(R.id.phoneState, pi);

        return updateViews;

}

}

}

Upvotes: 0

Views: 93

Answers (1)

Budius
Budius

Reputation: 39836

still not sure on what are u asking, but I'll try to answer, if not what u want, please explain in a different way.

Intent are usually started immediatly. You create an Intent can start it by calling startService or startActivity, so for example:

context.startService(new Intent(context, ToggleService.class));

on both times you wrote the above code, the service ToggleService is immediatly started.

PendingIntent on the other hand are saved to be started at later time, so for example

updateViews.setOnClickPendingIntent(R.id.phoneState, pi);

on the above line, the AppWidget Broadcast is started when the user clicks on R.id.phoneState in your HomeScreen AppWidget.

Each PendingIntent is stored in the system with a certain ID, this ID is the requestCode parameter (you used zero on your code)... that means, that if you create a different PendingIntent with the same ID it will override it, meaning a different action will be started when the user clicks on R.id.phoneState

Upvotes: 1

Related Questions