Search code examples
androidandroid-appwidget

Android AppWigdet is not working on lollipop only


I've implemented a app widget using below code, and it's working perfectly excepts Android Lollipop. The scenario is i have created a service which is tracking time and update widget's RemoteView with a broadcast receiver.

public class Widget extends AppWidgetProvider {
@Override
public void onEnabled(Context context) {
    super.onEnabled(context);
    context.startService(new Intent(UpdateTimeService.UPDATE_TIME));
}

@Override
public void onDisabled(Context context) {
    super.onDisabled(context);
    context.stopService(new Intent(context, UpdateTimeService.class));
}

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

    context.startService(new Intent(UpdateTimeService.UPDATE_TIME));
}

/*clock service*/
public static final class UpdateTimeService extends Service {
    static final String UPDATE_TIME = "app.xxxxx.widget.action.UPDATE_TIME";

    private Calendar mCalendar;
    private final static IntentFilter mIntentFilter = new IntentFilter();

    static {
        mIntentFilter.addAction(Intent.ACTION_TIME_TICK);
        mIntentFilter.addAction(Intent.ACTION_TIME_CHANGED);
        mIntentFilter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
    }

    @Override
    public void onCreate() {
        super.onCreate();

        mCalendar = Calendar.getInstance();
        registerReceiver(new UpdateTimeReceiver(), mIntentFilter);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        unregisterReceiver(new UpdateTimeReceiver());
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        super.onStartCommand(intent, flags, startId);

        if (intent != null) {
            if (UPDATE_TIME.equals(intent.getAction())) {
                updateTime(getApplicationContext());
            }
        }
        return START_STICKY;
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    public final class UpdateTimeReceiver extends BroadcastReceiver{

        @Override
        public void onReceive(Context context, Intent intent) {
            // TODO Auto-generated method stub

            updateTime(context);
        }

    }

    private void updateTime(Context context) {
        mCalendar.setTimeInMillis(System.currentTimeMillis());

        RemoteViews mRemoteViews = new RemoteViews(getPackageName(),
                R.layout.widget);
        try {
            String time = DateFormat.format("h:mm",mCalendar).toString();

            mRemoteViews.setTextViewText(R.id.Time, time);

            updateWeather(mRemoteViews, context);

            ComponentName mComponentName = new ComponentName(this,
                    Widget.class);
            AppWidgetManager mAppWidgetManager = AppWidgetManager
                    .getInstance(this);
        mAppWidgetManager.updateAppWidget(mComponentName,mRemoteViews);
        } catch (Exception err) {
            Toast.makeText(context, "Error", Toast.LENGTH_LONG).show();
        }
    }
}

}

/*Here is the manifest declaration */

<receiver
        android:name="app.xxxxx.widget.Widget"
        android:label="@string/widget_name" >
        <meta-data
            android:name="android.appwidget.provider"
            android:resource="@xml/clock_widget" />

        <intent-filter>
            <action android:name="app.xxxxx.widget.action.UPDATE_TIME" />
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
            <action android:name="android.intent.action.TIME_SET" />
            <action android:name="android.intent.action.BOOT_COMPLETED" />
            <action android:name="android.intent.action.TIME_TICK" />
            <action android:name="android.intent.action.TIMEZONE_CHANGED" />
        </intent-filter>
    </receiver>

    <service
        android:name="app.xxxxx.widget.Widget$UpdateTimeService"
         >
        <intent-filter>
            <action android:name="app.xxxxx.widget.action.UPDATE_TIME" />
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
            <action android:name="android.intent.action.TIME_SET" />
            <action android:name="android.intent.action.TIME_TICK" />
            <action android:name="android.intent.action.TIMEZONE_CHANGED" />
        </intent-filter>
    </service>

    <receiver
        android:name="app.xxxxx.widget.Widget$UpdateTimeService$UpdateTimeReceiver"
        android:process=":track_time_update" >
        <intent-filter>
            <action android:name="app.xxxxx.widget.action.UPDATE_TIME" />
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
            <action android:name="android.intent.action.TIME_SET" />
            <action android:name="android.intent.action.TIME_TICK" />
            <action android:name="android.intent.action.TIMEZONE_CHANGED" />
        </intent-filter>
    </receiver>

Is there any other configuration that i needed for Lollipop version


Solution

  • After suffering whole 9 days i've found a illogical solution by myself, I simply changed the targetSdkVersion from 22 to 19 then it's works on Lolipop.