Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Making a phone call from home screen widget

I have a widget and I want it to make a phonecall to a particular number when the user clicks on the widget. How do i do this? Please help.

like image 783
Rohith Nandakumar Avatar asked Dec 21 '22 21:12

Rohith Nandakumar


2 Answers

    public static Intent newPhoneCallIntent(String phoneNumber){
     Intent callintent = new Intent(Intent.ACTION_DIAL);
     callintent.setData(Uri.parse("tel:"+phoneNumber));
     return callintent;
    }
    startActivity(newPhoneCallIntent("5555555555"));
like image 42
Nathan Schwermann Avatar answered Dec 24 '22 11:12

Nathan Schwermann


I was able to get it working with this code:

 @Override
 public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    Log.d(LOG_TAG, "onUpdate(): ");
    for (int appWidgetId : appWidgetIds) {

        Intent callIntent = new Intent(Intent.ACTION_CALL);
        callIntent.setData(Uri.parse("tel:"+number));
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, callIntent, 0);

        // Get the layout for the App Widget and attach an on-click listener to the button
        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.caller);
        views.setOnClickPendingIntent(R.id.callButton, pendingIntent);

        // Tell the AppWidgetManager to perform an update on the current App Widget
        appWidgetManager.updateAppWidget(appWidgetId, views);
    }
}
like image 130
Rohith Nandakumar Avatar answered Dec 24 '22 10:12

Rohith Nandakumar