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;
}
Oded
  • 489,969
  • 99
  • 883
  • 1,009
subrussn90
  • 1,142
  • 1
  • 14
  • 27
  • You would probably want to create a Service for that widget. If I get you right and use it to update your widget. – Sergey Benner Jan 11 '12 at 12:38
  • yes....this is the Service i have defined to update my widget... – subrussn90 Jan 11 '12 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. – Sergey Benner Jan 11 '12 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? – subrussn90 Jan 11 '12 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 http://stackoverflow.com/questions/2748590/clickable-widgets-in-android – Sergey Benner Jan 11 '12 at 12:56
  • More to that http://stackoverflow.com/questions/5347986/android-widget-on-click-event – Sergey Benner Jan 11 '12 at 12:57

0 Answers0