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

发布于 2024-10-02 15:02:26 字数 49 浏览 5 评论 0原文

我有一个小部件,我希望它在用户单击该小部件时向特定号码拨打电话。我该怎么做?请帮忙。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

狼亦尘 2024-10-09 15:02:26

我能够使用以下代码让它工作:

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

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);
    }
}
蓝天 2024-10-09 15:02:26
    public static Intent newPhoneCallIntent(String phoneNumber){
     Intent callintent = new Intent(Intent.ACTION_DIAL);
     callintent.setData(Uri.parse("tel:"+phoneNumber));
     return callintent;
    }
    startActivity(newPhoneCallIntent("5555555555"));
    public static Intent newPhoneCallIntent(String phoneNumber){
     Intent callintent = new Intent(Intent.ACTION_DIAL);
     callintent.setData(Uri.parse("tel:"+phoneNumber));
     return callintent;
    }
    startActivity(newPhoneCallIntent("5555555555"));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文