屏幕接收器广播无法正常工作

时间:2016-12-28 08:01:09

标签: android broadcastreceiver android-service

我希望在Android中关闭屏幕时收到广泛的演员。我写了以下代码。这是接收者的代码。

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

    helper = new FeedReaderDBHelper(context);
    if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
        System.out.println("Screen Off Receiver just received something");
        if(helper.getValue(FeedReaderContract.FeedEntry.onlock).equals("YES"))
        {

            helper.lockAll();
            wasScreenOn = false;
        }
        else if(helper.getValue(FeedReaderContract.FeedEntry.onLock3).equals("YES"))
        {
            System.out.println("Running 3 minutes thread");
            Runner runner = new Runner(context);
            Thread t = new Thread(runner);
            t.setName("LockThreeThread");

            t.start();
        }
    } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
        // and do whatever you need to do here
        wasScreenOn = true;
    }
}

我正在服务中注册和取消注册Receiver。代码如下。

onStartCommand方法中的Registring Receiver

 @Override
public int onStartCommand(Intent intent, int flags, int startId) {
    pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
    isScreenOn = pm.isScreenOn();
    RegisterReceiver();
    //Toast.makeText(getApplicationContext(), "Service Started", Toast.LENGTH_SHORT).show();
    //System.out.println("StartRemove");
    helper = new FeedReaderDBHelper(this);
    names = helper.getNames();
    Thread t = new Thread(new Runnable() {
        @Override
        public void run() {
            startChecker();
        }
    });
    t.start();

    return START_STICKY;
}

这就是我在服务方法的onDestroy中取消注册的方式。

private void RegisterReceiver()
{
    receiver = Factory.getScreeReceiver();
    IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_OFF);
    filter.addAction(Intent.ACTION_SCREEN_ON);
    registerReceiver(receiver, filter);
}

请让我知道可能是什么问题。我已经挖了很长时间了。最近两天被浪费了。似乎没什么用。

1 个答案:

答案 0 :(得分:0)

我认为你过于复杂了。 这是您需要的活动

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Intent startServiceIntent = new Intent(this, MyService.class);
    startService(startServiceIntent);

}

这是服务

public class MyService extends Service {
public MyService() {
}

@Override
public IBinder onBind(Intent intent) {
    // TODO: Return the communication channel to the service.
    throw new UnsupportedOperationException("Not yet implemented");
}


@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Log.i("APP", "Service started");
    ScreenOffReceiver receiver = new ScreenOffReceiver();
    registerReceiver(receiver, new IntentFilter(Intent.ACTION_SCREEN_OFF));

    return START_STICKY;
}
}

这是广播接收器

public class ScreenOffReceiver extends BroadcastReceiver {
    public ScreenOffReceiver() {
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i("APP", "EVENT occured");
    }
}

我刚尝试了这段代码,日志工作正常。

当您通过服务启动它,并启动一个新线程来执行onStartCommand()方法中的工作时,请注意每次调用活动的onCreate()时线程都会启动。

希望这有帮助