2015-03-24 58 views
0

爲什麼我的廣播接收機只發送和創建通知? 我試圖讓它自動打開設備 但由於某種原因設備狀態沒有改變。廣播接收機不觸發switchon方法

它適用於在其他類中觸發但不在廣播中。

請幫我找到解決辦法:)

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

你的意思是廣播接收機? – powerfj 2015-03-25 00:03:13

+0

你是否也在清單文件中啓用接收器? – 2015-03-25 04:19:23

+0

我已經定義了它,但實際上並未啓用它。即時通訊使用過濾器,所以我認爲它不需要在清單。廣播會創建通知,但不會觸發設備狀態。你仍然認爲問題在於啓用它嗎?你能舉個例子:) – Abby 2015-03-25 04:49:41

回答

0

如果你的意思BroadcastReceiver,它取決於接收器的性質。

通常,僅在onReceive()中使用該代碼是正確的答案,特別是對於清單註冊的接收器,因爲它們只能用於一次廣播。

如果您使用registerReceiver()註冊接收器,歡迎您創建一個構造函數,如果你喜歡。

+0

謝謝你的回答。根據你所說的,我做到了。但它並未觸發該方法。請檢查代碼是否可以 – Abby 2015-03-25 01:30:49

+0

@Abby:由於您似乎沒有註冊接收器,因此它不會收到任何內容。 – CommonsWare 2015-03-25 10:37:52

+0

我確實在清單中啓用它,如果你的意思是。你能解釋一下嗎? – Abby 2015-03-25 13:49:46