Android小部件按钮在几个小时后停止工作 - 为什么?

时间:2014-01-09 11:18:49

标签: android button widget

我有一个小部件,每天都会显示一个圣经的引用,并带有一个简单的刷新按钮。但几个小时后按钮停止工作,我无法弄清楚原因。

可能有人建议快速解决此问题...... 谢谢!

package com.example.napiige;

import java.util.Calendar;
import java.util.Random;

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


public class MainActivity extends AppWidgetProvider {   

    String azIge;   
    static MainActivity[] ige = new MainActivity[3];
    public static String WIDGET_BUTTON = "MY_PACKAGE.WIDGET_BUTTON";

    @Override
     public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {         
        final int N = appWidgetIds.length;
        // Perform this loop procedure for each App Widget that belongs to this provider
        for (int i=0; i<N; i++) {
        int appWidgetId = appWidgetIds[i];

        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.example_appwidget);
        RemoteViews viewsT = new RemoteViews(context.getPackageName(), R.layout.example_appwidget);

        Intent intent = new Intent(WIDGET_BUTTON);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        views.setOnClickPendingIntent(R.id.refresh_btn, pendingIntent );    

        Calendar calendar = Calendar.getInstance();
        // 8 AM 
    //  calendar.set(Calendar.HOUR_OF_DAY, 8);
    //  calendar.set(Calendar.MINUTE, 0);
    //  calendar.set(Calendar.SECOND, 0);

        AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        //am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
        am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 240*1000, pendingIntent);


        String ig1 = context.getString(R.string.ige1);
        String ig2 = context.getString(R.string.ige2);
        String ig3 = context.getString(R.string.ige3);
        String ig4 = context.getString(R.string.ige4);
        String ig5 = context.getString(R.string.ige5);
        String ig6 = context.getString(R.string.ige6);

        ige = new MainActivity[6];

        ige[0] = new MainActivity();
        ige[1] = new MainActivity();
        ige[2] = new MainActivity();
        ige[3] = new MainActivity();
        ige[4] = new MainActivity();
        ige[5] = new MainActivity();

        ige[0].azIge = ig1;
        ige[1].azIge = ig2;
        ige[2].azIge = ig3;
        ige[3].azIge = ig4;
        ige[4].azIge = ig5;
        ige[5].azIge = ig6;


        viewsT.setTextViewText(R.id.igePrint, ige[5].azIge);


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

        }
    }



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

        super.onReceive(context, intent);
        //Log.d("on recieve", "here");

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

        AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);

        ComponentName watchWidget;

        RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.example_appwidget);
        watchWidget = new ComponentName(context, MainActivity.class);

        Random r = new Random();
        int i1 = r.nextInt(6-0) + 0;      
        remoteViews.setTextViewText(R.id.igePrint, ige[i1].azIge);

        appWidgetManager.updateAppWidget(watchWidget, remoteViews);
        }
    }

}

0 个答案:

没有答案