1

I'm creating a widget for my application.I need to programatically make a call to a number which is get from a database.

I'm able to receive the database value and make the call.

I don't want to make the call while the widget is created....rather have to make calls whenever the application is clicked...

I'm posting my WidgetService class here...

@Override
public void onStart(Intent intent, int startId) {
    Log.i(LOG, "Called");


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


    for (int widgetId : allWidgetIds) {


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

        //remoteViews.setTextViewText(R.id.update,"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((Integer) null, pendingIntent);
        appWidgetManager.updateAppWidget(widgetId, remoteViews);

        myDB = UpdateWidgetService.this.openOrCreateDatabase("divertnumber", Context.MODE_PRIVATE, null);
        Cursor cursor = myDB.query("diverted", null, null, null, null, null, null);
        cursor.moveToLast();
        phoneNumber = cursor.getString(cursor.getColumnIndex("divertno"));
        cursor.close();
        Toast.makeText(UpdateWidgetService.this, "FROM DATABASE "+phoneNumber, Toast.LENGTH_LONG).show();


        //Uri uri = Uri.fromParts("tel", phoneNumber, null);

        Intent callIntent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"+5556+","+1+","+1)); 
        callIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(callIntent);
    }
    stopSelf();

    super.onStart(intent, startId);
}

@Override
public IBinder onBind(Intent intent) {
    return null;
}
6
  • You would probably want to create a Service for that widget. If I get you right and use it to update your widget. Commented Jan 11, 2012 at 12:38
  • yes....this is the Service i have defined to update my widget... Commented Jan 11, 2012 at 12:40
  • The application and your widget are connected via database at this point so it depends on your service how often you will get updates for your widget. You can implement a start of your application upon a widget click then do your stuff there and the service will update widget when it sees the changes. This is normal behavior for UI->Service->Widget. Commented Jan 11, 2012 at 12:44
  • I really want to make an Emergency call via the widget....but the call is initiating at the instance the widget is created.........what to do? Commented Jan 11, 2012 at 12:48
  • 1
    Hmmm. The service updates the widget with needed info -> the widget reflects it. You want to make a call upon a click on the widget? Start an Intent with a parameter from this Widget. More on clickable widgets here stackoverflow.com/questions/2748590/… Commented Jan 11, 2012 at 12:56

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.