简体   繁体   中英

notify() method not been called form when calling it from another class with synchronized()

I'm trying to fire a notification when the battery level is less the %10.

here is my notification class located at MainActivity.java

    public void notify(View view) {
        String title = "Warning! low battery";
        String text = "Please plug your mobile device to a charger";

        Intent intent = new Intent(this, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,0);

        Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
                .setSmallIcon(R.drawable.ic_qs_battery_saver)
                .setContentTitle(title)
                .setContentText(text)
                .setContentIntent(pendingIntent)
                .build();

        notificationManager.notify(notificationID++, notification);
    }

I'm trying to fire it from the BatteryReceiver class:

package com.michal.ex2;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
import android.util.Log;
import android.view.View;

public class BatteryReceiver extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent)
    {
        String action = intent.getAction();
        if(intent.getAction().equals(Intent.ACTION_BATTERY_CHANGED)) {

            IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
            Intent batteryStatus = context.registerReceiver(null, ifilter);
            int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
            int scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, 100);
            int percent = (level * 100) / scale;

            int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
            boolean isPlugged;
            isPlugged = plugged == BatteryManager.BATTERY_PLUGGED_AC || plugged == BatteryManager.BATTERY_PLUGGED_USB;

            if ((percent == 10) && !isPlugged) {
                Log.d("mylog", "low battery");
                //notify();
                synchronized(mActivity){
                    mActivity.notify();
            }

        }

    }
}

First i tried calling calling notify from MainActivity, but the app crash with the error: java.lang.IllegalMonitorStateException: object not locked by thread before notify()

I've searched for a solution and found that I need to lock the notify, so I tried calling it with synchronized():

synchronized(mActivity){
          mActivity.notify();

but nothing happened. The battery receiver works alright(I'm getting the right log message) but notify() is not been called.

Maybe the function you'are calling is not the MainActivity.notify(View view) but the Object.notify() . Therefore nothing happens. Give an appropriate function argument.

mActivity.notify(null);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM