1

我正在尝试学习使用 Android BroadcasReceiver。我写了这段代码,但它不起作用......我试着改变时间等......它有什么问题?

我在清单中添加:

<receiver android:name="com.example.broadcastreceiverspike.Broadcast" >
        <intent-filter android:priority="100">
            <action android:name="android.intent.action.ACTION_SCREEN_ON" />
            <action android:name="android.intent.action.ACTION_SCREEN_OFF" />
            <action android:name="android.intent.action.TIME_SET" />
        </intent-filter>
</receiver>

我的简单广播接收器:

    public class Broadcast extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
           Log.d("BROADCAST", "It worked");
           Toast.makeText(context, "BROADCAST",  Toast.LENGTH_LONG).show();
        }
}

我的主要活动(默认主要活动)

public class MainActivity extends Activity {

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

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

}
4

4 回答 4

2

我解决了!

“与其他广泛使用的意图不同,对于 Intent.ACTION_SCREEN_OFF 和 Intent.ACTION_SCREEN_ON,您不能在 Android 清单中声明它们!我不确定具体原因,但它们必须在您的 JAVA 代码中的 IntentFilter 中注册”

http://thinkandroid.wordpress.com/2010/01/24/handling-screen-off-and-screen-on-intents/

显现

    <receiver android:name=".Broadcast" >
        <intent-filter>                
            <action android:name="android.intent.action.TIME_SET" />

        </intent-filter>
    </receiver>

主要活动课

公共类 MainActivity 扩展 Activity {

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

     // INITIALIZE RECEIVER
    IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
    filter.addAction(Intent.ACTION_SCREEN_OFF);
    BroadcastReceiver mReceiver = new Broadcast();
    registerReceiver(mReceiver, filter);

}

}

我的广播接收器

公共类广播扩展广播接收器{

public static String TAG = "BROADCAST";

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

    if (intent.getAction().equals(Intent.ACTION_TIME_CHANGED))
    {
        Log.d(TAG, "BROADCAST Cambio di orario");
        Toast.makeText(context, "BROADCAST Cambio di orario", Toast.LENGTH_LONG).show();
    }
    else if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
        // DO WHATEVER YOU NEED TO DO HERE
        Log.d(TAG, "BROADCAST Screen OFF");
        Toast.makeText(context, "Screen OFF", Toast.LENGTH_LONG).show();
    } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
        // AND DO WHATEVER YOU NEED TO DO HERE
        Log.d(TAG, "BROADCAST Screen ON");
        Toast.makeText(context, "BROADCAST Screen ON", Toast.LENGTH_LONG).show();
    }
}

}

于 2013-07-04T14:29:57.580 回答
0

您是否添加了所需的权限?

使用权限 android:name="android.permission.READ_PHONE_STATE"

这是屏幕关闭或打开或锁定时要处理的接收器:

public class ScreenReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent)
{
    if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF))
    {   



    }
    else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON))
    {


    }
    else if(intent.getAction().equals(Intent.ACTION_USER_PRESENT))
    {

    }

}

这是清单:

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

        </intent-filter>
    </receiver>
于 2013-07-04T10:28:36.847 回答
0

查看有关广播接收器的本教程。

更新: 我不确定你是否在使用本教程,因为本教程中有很多有用的东西,比如:

@Override
public void onResume() {
    super.onResume();

    // Register mMessageReceiver to receive messages.
    LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver,
        new IntentFilter("my-event"));
    }

    // handler for received Intents for the "my-event" event 
    private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
        // Extract data included in the Intent
        String message = intent.getStringExtra("message");
        Log.d("receiver", "Got message: " + message);
    }
};

@Override
protected void onPause() {
    // Unregister since the activity is not visible
    LocalBroadcastManager.getInstance(this).unregisterReceiver(mMessageReceiver);
    super.onPause();
} 

换句话说,您已经创建了一个 custom broadcast receiver class,但您还没有使用它。

也请检查这个

于 2013-07-04T10:15:58.570 回答
0

我遇到了同样的问题并修复了它(在 4.3 和 5.1 上测试)。我可以在清单中声明“android.intent.action.USER_PRESENT”,只要你有 READ_PHONE_STATE 权限,就可以了!!我的迷你应用程序包含一个广播接收器,它对屏幕的开/关状态做出反应,并运行一个后台服务来进行连续的语音识别。如果屏幕关闭,则识别将关闭。这是代码,享受:清单:

<uses-permission android:name="android.permission.READ_PHONE_STATE"/> <receiver android:name="classes.VoiceLaunchReceiver" >
            <intent-filter>                
                <action android:name="android.intent.action.USER_PRESENT" />    
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </receiver>

广播接收器:

public class VoiceLaunchReceiver extends BroadcastReceiver {
    @Override  
    public void onReceive(Context ctx, Intent intent) {     
        Intent service = new Intent(ctx, VoiceLaunchService.class);
     //   service.putExtra(action, true);
        Log.i("joscsr","Incoming Voice Launch Broadcast...");  

        if (intent.getAction().equals(Intent.ACTION_USER_PRESENT)) {
            Log.i("joshcsr", "************\nCSR Resumed (BC)\n************");
            ctx.startService(service);
            }
        if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
            Log.i("joshcsr", "************\nCSR STOPPED by SCREEN (BC)\n************");
            ctx.stopService(service);  
            }
        }  
}

可以想象,我的 USER_PRESENT 广播接收器没有在其他任何地方注册。我确实在我的服务的 onCreate 方法中注册了 ACTION_SCREEN_OFF 和 ON,该方法由我的接收器触发。

@Override
public void onCreate() {
    super.onCreate();
    //Register screen ON/OFF BroadCast
    launcher=new VoiceLaunchReceiver();
    IntentFilter i=new IntentFilter(Intent.ACTION_SCREEN_OFF);
    i.addAction(Intent.ACTION_SCREEN_ON);               
    registerReceiver(launcher,i);
    Log.d("joshcsr","VoiceLaunch Service CREATED"); 
    }

最后,我在我的服务的 onDestroy() 中取消注册屏幕开/关:

@Override
    public void onDestroy() {
        super.onDestroy();
        unregisterReceiver(launcher);}
于 2015-03-17T15:26:05.053 回答