简体   繁体   English

当从另一个 class 使用 synchronized() 调用 notify() 方法时,它没有被调用形式

[英]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.我正在尝试在电池电量低于 %10 时发出通知。

here is my notification class located at MainActivity.java这是我的通知 class 位于 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:我正在尝试从 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()首先,我尝试从 MainActivity 调用通知,但应用程序崩溃并出现错误: 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() 调用它:

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.电池接收器工作正常(我得到了正确的日志消息),但没有调用 notify()。

Maybe the function you'are calling is not the MainActivity.notify(View view) but the Object.notify() .也许您调用的 function 不是 MainActivity.notify(View view) 而是Object.notify() Therefore nothing happens.因此什么也没有发生。 Give an appropriate function argument.给出适当的 function 参数。

mActivity.notify(null);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当一个线程通过另一个类调用synchronized方法时会发生什么? - What happens when a synchronized method is called by a thread via another class? 在另一个方法被调用后调用方法时是否存在nullpointerexception? - nullpointerexception when calling method after another method has been called? 从Java中同一类的另一个同步方法内部创建的新线程中调用同步方法 - Calling a synchronized method from a new thread created inside another synchronized method of the same class in Java 从另一个同步方法调用同步方法,两者都在不同的 object - Calling a synchronized method from another synchronized method, both on different object 使用synchronized但从结果IllegalMonitorStateException通知来自另一个类的线程 - Notify Thread from another class using synchronized but result IllegalMonitorStateException 调用同步方法时线程调用非同步实例方法 - Thread calling non-synchronized instance method when a synchronized method is called 同步方法调用另一个调用wait()的同步方法 - Synchronized method calling another synchronized method which calls wait() 从另一个类调用方法时,是否必须在调用该方法的类中实例化该方法中的变量? - When calling a method from another class, do variables in that method have to be instantiated in the class calling the method? 从另一个类调用方法 - Calling a method from another class 从另一个类调用时,实例“ Main”方法未运行 - Instance “Main” Method Not Running When Called from Another Class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM