为什么我的Screen On / Off BroadcastReceiver没有工作?

时间:2016-04-07 06:07:14

标签: java android android-broadcastreceiver

这是我的Receiver文件。我觉得没有任何问题。我只打印传递的意图类型。根据我的onReceive从未被称为。我想我的清单本身有问题吗?还是其他任何想法? Receiver未与应用程序中的任何其他代码链接。

    l_query = "SELECT creationdate AS " + m_dateFrom.getValue(); 
                l_query += "AND creationdate AS"  + m_dateTo.getValue(); 
                l_query += "from tablename";

    Error i am getting is:

     org.hibernate.QueryException: Not all named parameters have been set: [20:53] [SELECT creationdate AS Tue Mar 01 10:20:53 IST 2016AND creationdate ASThu Apr 07 10:20:53 IST 2016from tablename]

我的Manifest看起来也不错,但我无法弄清楚它为什么不起作用。

package sharukh.locky;

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

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

    @Override
    public void onReceive(Context context, Intent intent)
    {
        Log.i("rece", intent.getAction());
    }
}

我没有动态添加/注册/取消注册任何接收者。我需要一些帮助。

1 个答案:

答案 0 :(得分:1)

您可以尝试动态调用广播,如下所示

public class YourActivity extends Activity {

    //Create broadcast object
    BroadcastReceiver mBroadcast = new BroadcastReceiver() {    
    //When Event is published, onReceive method is called
    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        Log.i("[BroadcastReceiver]", "MyReceiver");

        if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
            Log.d("[BroadcastReceiver]", "Screen ON");
        } else if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
            Log.d("[BroadcastReceiver]", "Screen OFF");
        }

    }
};

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

        registerReceiver(mBroadcast, new IntentFilter(Intent.ACTION_SCREEN_ON));
        registerReceiver(mBroadcast, new IntentFilter(Intent.ACTION_SCREEN_OFF));
    }
}