1

I am creating a simple widget that shows battery percentage. It updates its status every 30 minutes. It's working fine. But, i also want to update its status if user clicks on the widget. I have implemented the code for it, its working fine on Emulator (Genymotion - Samsung Galaxy S3 - Android 4.1.2) & on a real device (Motorola Moto E - Android 4.4.2). But, the onClick part is not working for Nokia X. There's also no errors in logcat.

I tried to set the listener on different elements (imageview, layout, textview, etc), but the intent is not getting called on click. I also checked the other 3rd party widgets (Whatsapp, Clock) & their on click event is working fine. So, what could be the reason behind this?

The code is used for setting onclick intent :

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

    for(int i=0; i<appWidgetIds.length;i++) {
        int appWidgetId = appWidgetIds[i];
        Intent batteryCheckerIntent = new Intent(context, Main.class);
        batteryCheckerIntent.setAction(WIDGET_CLICKED);

        batteryCheckerIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);

        PendingIntent batteryCheckerPendingIntent = PendingIntent.getBroadcast(
                context, 0, batteryCheckerIntent,
                PendingIntent.FLAG_CANCEL_CURRENT);

        RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.main);

        remoteViews.setOnClickPendingIntent(R.id.batteryStatusImageView, batteryCheckerPendingIntent);

        appWidgetManager.updateAppWidget(appWidgetId, remoteViews);         
    }       
}

@Override
public void onReceive(Context context, Intent intent) {
    super.onReceive(context, intent);
    Log.i(this.getClass().getName(), "OnReceive is called");    
    if(intent.getAction()==WIDGET_CLICKED) {

        Intent batteryCheckerIntent = new Intent(context, BatteryLevelCheckerService.class);
        context.startService(batteryCheckerIntent);

    } else if(intent.getAction()=="com.ashishtanna.batterystatus.CHECK_BATTERY_LEVEL") {
        Log.i("CHECK BATTERY", "CHECK BATTERY called");

        //If i uncomment the below code, the intent set for onclick event (in the if part) will stop working, it won't even call the onReceive method for onClick

        /*Intent batteryCheckerIntent = new Intent(context, BatteryLevelCheckerService.class);
        context.startService(batteryCheckerIntent);*/
    }
}

UPDATE :

I tried everything i could, but it is not working on Nokia X platform. It looks like a bug because it is allowing only one intent to run. If i set intent for opening a service every 30 minutes, it will stop the intent set for onclick event and if i remove it, the onclick intent starts working again. (Now, reporting this problem/bug to Nokia.)

Ashish Tanna
  • 645
  • 1
  • 10
  • 25

0 Answers0