2015-03-25 3 views
0

Почему мой вещательный приемник только отправляет и создает уведомления? Я пытаюсь включить его автоматически, но состояние устройства по какой-то причине не меняется.Приемник вещания не запускается

Он работает, если запускается в других классах, но не в трансляции.

пожалуйста, помогите мне найти решение :)

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); 
     } 
    } 
} 
+0

привет и может PLS размещать широковещательный декларацию манифеста? – user1140237

+0

использовать контекст вместо context.getApplicationContext(). и попробуйте распечатать что-то в DeviceState(). – Harry

+0

Я пробовал использовать контекст, но ничего не изменилось. распечатать что-нибудь? – Abby

ответ

0

Had вы упоминали приемник в файле манифеста, как это:

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

Да, я сделал. (Без enabled = true). Tho я использовал фильтр, поэтому его не нужно в манифесте, я предполагаю ... Уведомление создается, но состояние устройства не запускается – Abby

+0

сделать его включенным также в манифесте и попробовать –

+0

Я попробовал, но ничего не изменилось – Abby