Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/221.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 可点击的小部件按钮不';单击时不做任何事情_Android_Widget - Fatal编程技术网

Android 可点击的小部件按钮不';单击时不做任何事情

Android 可点击的小部件按钮不';单击时不做任何事情,android,widget,Android,Widget,我有一个小部件,它在屏幕上安装OK,但当我按下它上面的按钮时,它应该在我的项目中打开一个活动。我什么都试过了,但还是不行 这是我的密码: public class MyWidgetProvider extends AppWidgetProvider { public static String INTENT_ACTION="OPEN_SECOND_VIEW_ACTIVITY"; @Override public void onUpdate(Context contex

我有一个小部件,它在屏幕上安装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();
        }
    }
}
这是我的舱单

<?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>

小部件信息

<?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\u布局

<?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>

您的代码的问题在于
pendingent
以错误的方式创建。根据代码,您似乎希望在
AppWidgetProvider
中接收该意图。如果是这样,那么您需要为intent提供providers类(而不是activity类):

并获得广播挂起内容,而不是活动一:

PendingIntent pendingIntent = PendingIntent.getBroadcast(context, appWidgetId, intent, PendingIntent.FLAG_UPDATE_CURRENT);
有关其用法的更多详细信息,请参阅文档

下面是通过单击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);
        }
    }
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, appWidgetId, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    @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);
        }
    }