注册广播接收器android的正确方法

时间:2015-08-17 20:33:16

标签: android broadcastreceiver

在我的Android应用程序中,我正在注册多个广播接收器,包括:WiFi,屏幕和活动。该应用程序在Moto g和三星G3上运行顺畅,但在三天顺利执行后,它停止感应屏幕开/关,Wifi连接/断开连接以及另一部手机上的活动事件。由于应用程序在用户手机上,我不知道模型/制造。除了事件感应之外,通常的位置感应工作正常(每10分钟安排一次)。什么想法可能是错的?否则,在我的测试设备上它仍然正常运行(测试超过2周)。作为参考,以下是仅用于屏幕传感器的代码。另外,我尝试在onstart命令中注册接收器,但它在我的测试设备上没有工作(参见screenService中的注释部分)

MainActivity:

Intent intentscreen = new Intent(getApplicationContext(), ScreenService.class);
startService(intentscreen);

ScreenService:

    private final BroadcastReceiver mybroadcast = new ScreenReceiver();
    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId)
    {
        /* if (intent!=null)
        {
            IntentFilter screenStateFilter = new IntentFilter();
            screenStateFilter.addAction(Intent.ACTION_SCREEN_ON);
            screenStateFilter.addAction(Intent.ACTION_SCREEN_OFF);
            registerReceiver(mybroadcast, screenStateFilter);
        }*/
        return START_STICKY;
    }
    @Override
    public void onCreate (){
        super.onCreate();

        IntentFilter screenStateFilter = new IntentFilter();
        screenStateFilter.addAction(Intent.ACTION_SCREEN_ON);
        screenStateFilter.addAction(Intent.ACTION_SCREEN_OFF);
        registerReceiver(mybroadcast, screenStateFilter);
    }

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

ScreenReceiver:

public class ScreenReceiver extends BroadcastReceiver {

       @Override
       public void onReceive(Context context, Intent intent) {
           if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
               Log.i("Check","Screen went OFF");
            } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
                Log.i("Check","Screen went ON");

       }
    }

2 个答案:

答案 0 :(得分:1)

这些android.intent.action.SCREEN_ON android.intent.action.SCREEN_OFF android.intent.action.BATTERY_CHANGED android.intent.action.CONFIGURATION_CHANGED android.intent.action.TIME_TICK 必须动态注册

import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity {

BroadcastReceiver mybroadcast;
IntentFilter screenStateFilter;
static boolean alreadyListening = false; 
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mybroadcast = new ScreenBroadcastReceiver();
    screenStateFilter = new IntentFilter();
    screenStateFilter.addAction(Intent.ACTION_SCREEN_ON);
    screenStateFilter.addAction(Intent.ACTION_SCREEN_OFF);
If (!alreadyListening)
{
    registerReceiver(mybroadcast, screenStateFilter);
alreadyListening = true;
}


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
}

编辑: MainActivity.java     包com.kishore_kumar.stackoverflow;

package com.kishore_kumar.stackoverflow;


import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;


public class ScreenBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
        Log.e("Check", "Screen went OFF");
    } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
        Log.e("Check","Screen went ON");
    }
}
}

ScreenBroadcastReceiver.java

SELECT bloglikes.post, blogdata.id
FROM bloglikes
LEFT JOIN blogdata
ON bloglikes.post=blogdata.id

答案 1 :(得分:-2)

你可以这样做:

清单文件中定义 Receiver

<receiver android:name=".ScreenOnOffListener" >
   <intent-filter>
     <action android:name="android.intent.action.SCREEN_OFF" />
     <action android:name="android.intent.action.SCREEN_ON" />
    </intent-filter>
</receiver>

现在添加扩展 BroadcastReceiver

public class ScreenOnOffListener extends BroadcastReceiver {

       @Override
       public void onReceive(Context context, Intent intent) {
           if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
               Log.i("Check","Screen OFF");
            } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
                Log.i("Check","Screen ON");
            }
    }

希望这会对你有所帮助。