Android - 我无法使小部件可点击以启动意图

时间:2010-05-23 21:48:32

标签: android

我是Android开发的新手。

我开发了一个非常简单的小部件,旨在通过ImageButton与用户进行交互。

我现在要做的是如下。当用户点击按钮(将小部件添加到他们的主屏幕后),我希望手机拨打某个电话号码。 一种主屏幕的快速拨号。

不幸的是,当我点按按钮时没有任何反应。

这是我的SpeedDialAppWidgetProvider.onUpdate方法的主体:

 Log.d("", "beginning of onUpdate");

 final int N = appWidgetIds.length;

    for (int i=0; i<N; i++) {
        int appWidgetId = appWidgetIds[i];

       Log.d("", "dealing with appWidgetId: " + appWidgetId);

        // Create an Intent to launch ExampleActivity
        Intent dialIntent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:1234567"));
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, dialIntent, 0); 


       Log.d("", "pendingIntent classname " + pendingIntent.getClass().getName());

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

       Log.d("", "remoteViews classname " + remoteViews.getClass().getName());

        // Tell the AppWidgetManager to perform an update on the current App Widget
        appWidgetManager.updateAppWidget(appWidgetId, remoteViews);   

        Log.d("", "end of onUpdate");

我可以看到调用该方法,并且日志记录的结​​果是有意义的。

speed_dial_appwidget.xml文件是这样的:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
androidrientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>

<ImageButton id="@+id/dial_icon"
android:src="@drawable/speed_dial"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/> 
</LinearLayout>

你能帮我解决这个问题吗?

提前致谢, 丹


感谢您的回答。

你的观点是有效的。然后我把它添加到我的清单中:

<uses-permission android:name="android.permission.CALL_PHONE" />

但这没有帮助。

我得到的唯一错误是:

05-23 18:41:08.932: ERROR/ActivityThread(655): Failed to find provider info for android.server.checkin
05-23 18:41:11.761: ERROR/ActivityThread(655): Failed to find provider info for android.server.checkin
05-23 18:41:14.372: ERROR/ActivityThread(655): Failed to find provider info for android.server.checkin
05-23 18:41:14.781: ERROR/ActivityThread(655): Failed to find provider info for android.server.checkin
05-23 18:41:43.892: ERROR/ActivityThread(676): Performing pause of activity that is not resumed: {com.android.launcher/com.android.launcher.Launcher}
05-23 18:41:43.892: ERROR/ActivityThread(676): java.lang.RuntimeException: Performing pause of activity that is not resumed: {com.android.launcher/com.android.launcher.Launcher}
05-23 18:41:43.892: ERROR/ActivityThread(676):     at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2809)
05-23 18:41:43.892: ERROR/ActivityThread(676):     at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2797)
05-23 18:41:43.892: ERROR/ActivityThread(676):     at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:2780)
05-23 18:41:43.892: ERROR/ActivityThread(676):     at android.app.ActivityThread.access$2000(ActivityThread.java:112)
05-23 18:41:43.892: ERROR/ActivityThread(676):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1699)
05-23 18:41:43.892: ERROR/ActivityThread(676):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-23 18:41:43.892: ERROR/ActivityThread(676):     at android.os.Looper.loop(Looper.java:123)
05-23 18:41:43.892: ERROR/ActivityThread(676):     at android.app.ActivityThread.main(ActivityThread.java:3948)
05-23 18:41:43.892: ERROR/ActivityThread(676):     at java.lang.reflect.Method.invokeNative(Native Method)
05-23 18:41:43.892: ERROR/ActivityThread(676):     at java.lang.reflect.Method.invoke(Method.java:521)
05-23 18:41:43.892: ERROR/ActivityThread(676):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:782)
05-23 18:41:43.892: ERROR/ActivityThread(676):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540)
05-23 18:41:43.892: ERROR/ActivityThread(676):     at dalvik.system.NativeStart.main(Native Method)

奇怪的是:即使在添加uses-permission标记之前,我也没有看到任何关于没有足够权限的错误消息。

我不明白。

1 个答案:

答案 0 :(得分:0)

ACTION_CALL需要特殊许可,您可能没有。在LogCat中应该有一个警告 - 在Eclipse中使用adb logcat,DDMS或DDMS透视图来查看是否出现此警告。

您可以考虑切换到ACTION_DIAL,这不需要特殊许可,但它只是将电话号码放在拨号器中而不是实际拨打电话。