【问题标题】:Broadcast receiver not triggering广播接收器未触发
【发布时间】:2015-03-25 03:25:52
【问题描述】:

为什么我的广播接收器只发送和创建通知?我正在尝试让它自动打开设备,但设备状态由于某种原因没有改变。

如果在其他类中触发而不是在广播中触发,它会起作用。

请帮我找到解决办法:)

public class ProximityIntentReceiver extends BroadcastReceiver {

    public boolean isFirstRun = false;  //Set this to true when user initially saves location
    private static final int NOTIFICATION_ID = 1000;
    WemoActivity wemo = new WemoActivity();
    private WeMoSDKContext mWeMoSDKContext = null;
    private String DeviceName = "uuid:Socket-1_0-221412K1100F3A";

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

        String key = LocationManager.KEY_PROXIMITY_ENTERING;
        Boolean entering = intent.getBooleanExtra(key, false);
        SharedPreferences mprefs = context.getSharedPreferences("First time", Context.MODE_PRIVATE);
        boolean isFirstRun = mprefs.getBoolean("First time", false);

        if (entering) {
            Toast.makeText(context,"in the region"  ,Toast.LENGTH_LONG).show();
            Log.d(getClass().getSimpleName(), "Entering");
        } else {
            Log.d(getClass().getSimpleName(), "Exiting");
            Toast.makeText(context,"out of the region" ,Toast.LENGTH_LONG).show();
        }
        sendNotification(context);
        // automatically switch device state when enter/leave region
        DeviceState(context.getApplicationContext(), entering);

    }

    public void sendNotification(Context context) {
        // String extra=arg1.getExtras().getString("alert").toString();
        long when = System.currentTimeMillis();
        String message = "You are near your office/home.";

        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notification = new Notification(R.drawable.ic_launcher,message, when);
        String title = "Proximity Alert!"; 
        Intent notificationIntent = new Intent();
        // set intent so it does not start a new activity
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent intent = PendingIntent.getActivity(context, 0,notificationIntent, 0);
        notification.setLatestEventInfo(context, title, message, intent);
        notification.flags |= Notification.FLAG_AUTO_CANCEL;
        notification.flags |= Notification.FLAG_SHOW_LIGHTS;
        notification.ledARGB = Color.WHITE;
        notification.ledOnMS = 1500;
        notification.ledOffMS = 1500;
        notification.defaults = Notification.DEFAULT_ALL;
        notificationManager.notify(NOTIFICATION_ID, notification);
        return;
    }

    private void DeviceState(Context context, boolean ON) {
        mWeMoSDKContext = new WeMoSDKContext(context);
        mWeMoSDKContext.addNotificationListener(null);
        if(ON) {
            mWeMoSDKContext.setDeviceState(WeMoDevice.WEMO_DEVICE_ON, DeviceName);
        } else {
            mWeMoSDKContext.setDeviceState(WeMoDevice.WEMO_DEVICE_OFF, DeviceName);
        }
    }
}

【问题讨论】:

  • 您好,您可以发布清单的广播声明吗?
  • 使用上下文而不是 context.getApplicationContext()。并尝试在 DeviceState() 中打印一些内容。
  • 我尝试使用上下文,但没有任何改变。打印一些东西?
  • @Harry 我已经添加了打印,并在触发时打印出来。这是否意味着问题出在设备或其他方面?也许与 mWeMoSDKContext.setDeviceState(WeMoDevice.WEMO_DEVICE_ON, DeviceName);但它在任何其他课程中都可以正常工作。谢谢

标签: android broadcastreceiver


【解决方案1】:

你有没有在清单文件中提到过你的接收器:

 <receiver
            android:name="receiver_name"
            android:enabled="true" >

【讨论】:

  • 是的,我做到了。 (没有启用=真)。我使用了过滤器,所以我认为清单中不需要它...通知已创建,但未触发设备状态
猜你喜欢
  • 1970-01-01
  • 2022-07-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-20
  • 2013-03-05
相关资源
最近更新 更多