开发者

Android widget click not working after some action

i need help with a widget. I have created a simple app with a widget. Only functionality is that the user opens the app by clicking the widget (and the widget shows the current date). Problem is that the user is no longer able to open the widget by clicking it after a restart of the phone or a language change. I uploaded the whole project here.

Please help me, this problem stops me from publishing two apps on the market...

Thanks

Hellowidget.java:

public class HelloWidget extends AppWidgetProvider {

    public static String ACTION_WIDGET_CONFIGURE = "ConfigureWidget";
    public static String ACTION_WIDGET_RECEIVER = "ActionReceiverWidget";
    private static final int REQUEST_CODE_ONE = 10;
    String elso;
    public static String MY_WIDGET_UPDATE = "MY_OWN_WIDGET_UPDATE";

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
         super.onUpdate(context, appWidgetManager, appWidgetIds);

         final int N = appWidgetIds.length;
              for (int i=0; i<N; i++) {
                  int appWidgetId = appWidgetIds[i];
                  updateAppWidget(context, appWidgetManager, appWidgetId);
                  Toast.makeText(context, "onUpdate(): " + String.valueOf(i) + " : " +  String.valueOf(appWidgetId), Toast.LENGTH_SHORT).show();
              }

         Intent intent = new Intent(context, UpdateService.class);
         context.startService(intent);
         RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.main);
         Intent configIntent = new Intent(context, MainActivity.class);
         configIntent.setAction(ACTION_WIDGET_CONFIGURE);
         PendingIntent configPendingIntent = PendingIntent.getActivity(context, REQUEST_CODE_ONE, configIntent, PendingIntent.FLAG_UPDATE_CURRENT);
         remoteViews.setOnClickPendingIntent(R.id.ImageView01, configPendingIntent);
         appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);

    }

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

         if(MY_WIDGET_UPDATE.equals(intent.getAction())){

           Bundle extras = intent.getExtras();
           if(extras!=null) {
            AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
            ComponentName thisAppWidget = new ComponentName(context.getPackageName(), HelloWidget.class.getName());
            int[] appWidgetIds = appWidgetManager.getAppWidgetIds(thisAppWidget);
            if (appWidgetIds.length > 0) {

                 new HelloWidget().onUpdate(context, appWidgetManager, appWidgetIds);
            }

           }

         }
    }

    public static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
              int appWidgetId){

            }
}

UpdateService.java:

public final class UpdateService extends Service {

    String datetoday;

    @Override
    public void onStart(Intent intent, int startId) {

        final Calendar today = Calendar.getInstance(); 
        int year_tod = today.get(Calendar.YEAR); 
        int month_tod = today.get(Calendar.MONTH)+1; 
        int day_tod = today.get(Calendar.DAY_OF_MONTH); 

        if (month_tod<10) 
        {
            datetoday =  String.valueOf(year_tod) + ".0" + String.valueOf(month_tod) + "." + String.valueOf(day_tod);
        }
        if (day_tod<10)
        {
            datetoday =  String.valueOf(year_tod) + "." + String.valueOf(month_tod) + ".0" + String.valueOf(day_tod);
        }
        if ((month_tod<10) && (day_tod<10))
        {
            datetoday =  String.valueOf(year_tod) + ".0" + String.valueOf(month_tod) + ".0" + String.valueOf(day_tod);
        }

        RemoteViews updateViews = new RemoteViews(this.getPackageName(), R.layout.main);

        Date date = new Date();
        java.text.DateFormat format = SimpleDateFormat.getDateInstance(
                SimpleDateFormat.MEDIUM, Locale.getDefault());

        updateViews.setTextViewText(R.id.widget_textview, datetoday);   

        ComponentName thisWidget = new ComponentName(this, HelloWidget.class);
        AppWidgetManager manager = AppWidgetManager.getInstance(this);
        manager.updateAppWidget(thisWidget, updateViews);

    }
    public void onCreate(Intent intent, int startId) {

    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }
}

And there MainActivity.java for the activity but that does not important.

Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.bfarago.hellowidget"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">


        <receiver android:name=".HelloWidget" android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
            </intent-filter>
            <meta-data android:name="android.appwidget.provider" android:resource="@xml/hello_widget_provider" />
        </receiver>
        <service android:enabled="true" android:name=".UpdateService" />

        <activity android:name=".MainActivity"
                          android:label="@string/hello">
                <intent_filter>
                       <action android:name="android.intent.action.MAIN" />
                       <category android:name="android.intent.category.LAUNCHER" />
                </intent_filter>
        </activity>
    </application>
<uses-sdk android:minSdkVersion="3" />

</manifest>

widget provider:

<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:min开发者_运维百科Width="146dip"
android:minHeight="72dip"
android:updatePeriodMillis="1000"
android:initialLayout="@layout/main"
/> 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜