user1658222
user1658222

Reputation: 83

how to call an intent from widget

I'm new to android development. And I've been looking for a solution from last 1 week. I'm not able to find a solution for it.

I'm building a widget which has timer running. I want to do something like startActivity(intent) at a specific time or condition an android.app.Activity. What I m trying to test here is calling a number using Intent.Action_call every 30 secs My Apologies for bad code. I'm new still in learning phase.

Till now what I've tried is

public class widget extends AppWidgetProvider {
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
        int[] appWidgetIds) {
    Log.i("MyActivity", Integer.toString(appWidgetIds.length ));
    Log.i("MyActivity", "onUpdate(): ");
    for (int appWidgetId : appWidgetIds) {

        Timer timer = new Timer();
        timer.scheduleAtFixedRate(new timertick(context, appWidgetManager), 1, 1000);
    }



  }
}

class timertick extends TimerTask {

 public timertick(Context context, AppWidgetManager appWidgetManager) {
    cont = context;
    this.appWidgetManager = appWidgetManager;
    remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);

    thisWidget = new ComponentName(context, widget.class);
    count++;
        }


@SuppressWarnings("deprecation")
@Override
public void run() {
    Date dt= new Date();
    remoteViews.setTextViewText(R.id.textView1,
            "Time = " + format.format(dt));

    Log.i("MyActivity", Integer.toString(count));


    if (dt.getSeconds() % 30== 0) {// here if can be any conditions like dt="some day and time"
        Log.i("MyActivity", "entering");
        Intent callIntent = new Intent(Intent.ACTION_CALL);
        callIntent.setData(Uri.parse("tel:123456789"));

        // I want to do some thing like this startActivity(callIntent) unfortunately we cannnot use it with widget provider.
    }

    appWidgetManager.updateAppWidget(thisWidget, remoteViews);
   }
}

Looking forward for your inputs. Please suggest how do I achieve this.

Upvotes: 0

Views: 584

Answers (1)

Goran Horia Mihail
Goran Horia Mihail

Reputation: 3645

You can use the context that you receive in the constructor to call startActivity:

cont.startActivity(callIntent);

Upvotes: 1

Related Questions