单击小部件按钮在单击时不执行任何操作

时间:2014-06-21 18:11:24

标签: android widget

我有一个小部件可以在屏幕上安装OK,但是当我按下它上面的按钮时,它应该在我的项目中打开一个活动。我已经尝试了一切,但它仍然无法正常工作。

这是我的代码:

public class MyWidgetProvider extends AppWidgetProvider {

    public static String INTENT_ACTION="OPEN_SECOND_VIEW_ACTIVITY";

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager,
            int[] appWidgetIds) {
        final int N = appWidgetIds.length;

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

            Intent intent = new Intent(context, SecondViewActivity.class);
            intent.setAction(INTENT_ACTION);
            PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);

            RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
            views.setOnClickPendingIntent(R.id.widgetButton, pendingIntent);

            appWidgetManager.updateAppWidget(appWidgetId, views);
        }
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.v("MT","onReceive called");
        super.onReceive(context, intent);
        Log.v("MT","constructor passed");
        Log.v("MT",intent.getAction());
        if (intent.getAction().equals(INTENT_ACTION)) {
            Log.v("MT","works");
            Toast.makeText(context, "works", Toast.LENGTH_SHORT).show();
        }
    }
}

这里的活动(这不是我想打开的主要活动)

public class SecondViewActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.edit_dialog);

        Intent intent = getIntent();
        Bundle extras = intent.getExtras();
        int mAppWidgetId = -1;

        if (extras != null) {
            mAppWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
        }

        AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(getApplicationContext());
        RemoteViews views = new RemoteViews(getApplicationContext().getPackageName(), R.layout.widget_layout);
        appWidgetManager.updateAppWidget(mAppWidgetId, views);

        Intent resultValue = new Intent();
        resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
        setResult(RESULT_OK, resultValue);
        finish();
    }

这是我的清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.multitanteo"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        <receiver android:name="MyWidgetProvider">
            <intent-filter >
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
            </intent-filter> 
            <meta-data android:name="android.appwidget.provider"
                android:resource="@xml/widget_info"/>
         </receiver>   

        <activity
            android:name="com.example.multitanteo.MainActivity"
            android:label="@string/app_name" 
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.example.multitanteo.SecondViewActivity"
            android:label="@string/title_activity_second_view_acitivity" >

            <intent-filter >
                <action android:name="android.intent.action.SEND"/>
                <category android:name="android.intent.category.DEFAULT"/>
                <data android:mimeType="text/plain"/>

            </intent-filter>

            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_CONFIGURE"/>
            </intent-filter>

            <intent-filter >
                <action android:name="android.intent.action.myTimeTravel"/>
                <category android:name="android.intent.category.DEFAULT"/>
                <data android:mimeType="text/plain"/>
            </intent-filter>

        </activity>


    </application>

</manifest>

widget_info

<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
    android:minWidth="40dp"
    android:minHeight="40dp"
    android:updatePeriodMillis="86400000"
    android:initialLayout="@layout/widget_layout"
    android:previewImage="@drawable/ic_launcher"
    android:configure="com.example.multitanteo.SecondViewActivity"
    android:resizeMode="horizontal|vertical"
    android:widgetCategory="home_screen|keyguard"
    android:initialKeyguardLayout="@layout/widget_keyguard">


</appwidget-provider>

widget_layout

<?xml version="1.0" encoding="utf-8"?>

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="@dimen/widget_margin"
    android:id="@+id/widgetFrameLayout">

    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">


    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:text="Hello this is my widget"/>

    <Button
        android:id="@+id/widgetButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button" />

    </LinearLayout>    

</FrameLayout>

1 个答案:

答案 0 :(得分:0)

您的代码存在的问题是PendingIntent以错误的方式创建。根据代码,您希望在AppWidgetProvider中收到该意图。如果是这样,那么您需要为intent提供提供程序类(而不是活动类):

final Intent intent = new Intent(context, MyWidgetProvider.class);

获取广播 PendingIntent,而不是活动1:

PendingIntent pendingIntent = PendingIntent.getBroadcast(context, appWidgetId, intent, PendingIntent.FLAG_UPDATE_CURRENT);

有关其使用的其他详细信息,请参阅PendingIntent文档。

以下是从AppWidget点击活动启动的工作示例:

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        for (int appWidgetId : appWidgetIds) {
            updateAppWidget(context, appWidgetManager, appWidgetId);
        }
    }

    static void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId) {
        final Intent intent = new Intent(context, MyWidgetProvider.class);

        intent.setAction(INTENT_ACTION);

        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, appWidgetId, intent, PendingIntent.FLAG_UPDATE_CURRENT);

        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
        views.setOnClickPendingIntent(R.id.widgetButton, pendingIntent);

        // Tell the AppWidgetManager to perform an update on the current app widget
        appWidgetManager.updateAppWidget(appWidgetId, views);
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        super.onReceive(context, intent);

        if (intent.getAction().equals(INTENT_ACTION)) {
            Toast.makeText(context, "works", Toast.LENGTH_SHORT).show();

            final Intent activityIntent = new Intent(context.getApplicationContext(), MyActivity.class).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

            context.startActivity(activityIntent);
        }
    }