Android Widget按钮不起作用

时间:2014-03-26 22:50:35

标签: android android-widget widget android-appwidget

小部件只是一个文本视图和一个按钮。当用户单击该按钮时,将启动“新建”活动。我的按钮不是以某种方式打开新活动(基本上是空白活动)。以下是我的代码。

MainActivity

package com.example.widget_ex;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.widget.RemoteViews;


public class MainActivity extends AppWidgetProvider{
      @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, New.class);
              PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);

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

              appWidgetManager.updateAppWidget(appWidgetId, views);
            }
          }
}

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="34dp"
        android:layout_marginTop="25dp"
        android:text="TextView" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="19dp"
        android:text="Button" />

</RelativeLayout>

widgetprovider.xml

<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider 
   xmlns:android="http://schemas.android.com/apk/res/android" 
   android:minWidth="146dp" 
   android:updatePeriodMillis="0" 
   android:minHeight="146dp" 
   android:initialLayout="@layout/activity_main">

</appwidget-provider>

清单

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

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.widget_ex.New"
            android:label="@string/app_name">
        </activity>
         <receiver android:name=".MainActivity" >
            <intent-filter>
               <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
            </intent-filter>
            <meta-data android:name="android.appwidget.provider"
               android:resource="@xml/widgetprovider" />
         </receiver>
    </application>

</manifest>

New.class

package com.example.widget_ex;

import android.app.Activity;
import android.os.Bundle;

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

}

another.xml

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

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />

</LinearLayout>

1 个答案:

答案 0 :(得分:0)

未调用onUpdate()方法。您必须覆盖onUpdate()方法。