Android:从主屏幕小部件拨打电话
我有一个小部件,我想让它在用户单击小部件时拨打特定号码。我该怎么做?请提供帮助。我能够使用以下代码使其正常工作:Android:从主屏幕小部件拨打电话,android,widget,phone-call,Android,Widget,Phone Call,我有一个小部件,我想让它在用户单击小部件时拨打特定号码。我该怎么做?请提供帮助。我能够使用以下代码使其正常工作: public static Intent newPhoneCallIntent(String phoneNumber){ Intent callintent = new Intent(Intent.ACTION_DIAL); callintent.setData(Uri.parse("tel:"+phoneNumber)); return cal
public static Intent newPhoneCallIntent(String phoneNumber){
Intent callintent = new Intent(Intent.ACTION_DIAL);
callintent.setData(Uri.parse("tel:"+phoneNumber));
return callintent;
}
startActivity(newPhoneCallIntent("5555555555"));
@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()
函数中?startActivity()
抛出错误-类型小部件的startActivity(Intent)方法未定义,您需要从上下文中执行。如果是通过某种按钮进行操作,请尝试getContext()。startActivity()在您第一次添加小部件时是否有效?或者你必须先启动应用程序才能触发onUpdate?它会在第一时间工作<添加小部件时,将调用code>onUpdate()
。