简体   繁体   English

Android:从主屏幕小部件拨打电话

[英]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. 请帮忙。

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);
    }
}
    public static Intent newPhoneCallIntent(String phoneNumber){
     Intent callintent = new Intent(Intent.ACTION_DIAL);
     callintent.setData(Uri.parse("tel:"+phoneNumber));
     return callintent;
    }
    startActivity(newPhoneCallIntent("5555555555"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM