【问题标题】:Android: Making a phone call from home screen widgetAndroid:从主屏幕小部件拨打电话
【发布时间】:2010-11-15 04:28:22
【问题描述】:

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

【问题讨论】:

    标签: android widget phone-call


    【解决方案1】:

    我能够使用此代码使其工作:

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

    【讨论】:

    • 第一次添加小部件时是否有效?还是必须先启动应用才能触发 onUpdate?
    • 它会第一次工作。添加小部件时会调用onUpdate()
    【解决方案2】:
        public static Intent newPhoneCallIntent(String phoneNumber){
         Intent callintent = new Intent(Intent.ACTION_DIAL);
         callintent.setData(Uri.parse("tel:"+phoneNumber));
         return callintent;
        }
        startActivity(newPhoneCallIntent("5555555555"));
    

    【讨论】:

    • 我在哪里添加这个?在onUpdate() 函数中?
    • startActivity() 抛出错误 - The method startActivity(Intent) is undefined for the type Widget
    • @rohith 您需要根据上下文进行操作。如果从某种按钮尝试 getContext().startActivity()
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多