2017-08-26 135 views
2

我在後臺運行應用程序時使用了地理柵欄。設備重啓後無法使用。即使在cn1中重新啓動設備後,我如何才能使其工作?我認爲cn1推送通知使用RECEIVE_BOOT_COMPLETED權限來實現它。我們是否有一些內置函數可以用於其他用途,如我的情況?設備重啓後在後臺自動運行應用程序

代碼:

Geofence gf = new Geofence("test", loc, 100, 100000); 
LocationManager.getLocationManager().addGeoFencing(GeofenceListenerImpl.class, gf); 



public class GeofenceListenerImpl implements GeofenceListener { 
    @Override 
    public void onExit(String id) { 
    } 

    @Override 
    public void onEntered(String id) { 
     if(Display.getInstance().isMinimized()) { 
      Display.getInstance().callSerially(() -> { 
       Dialog.show("Welcome", "Thanks for arriving", "OK", null); 
      }); 
     } else { 
      LocalNotification ln = new LocalNotification(); 
      ln.setId("LnMessage"); 
      ln.setAlertTitle("Welcome"); 
      ln.setAlertBody("Thanks for arriving!"); 
      Display.getInstance().scheduleLocalNotification(ln, 10, LocalNotification.REPEAT_NONE); 
     } 
    }  
} 

更新:如何按照清單通過原生接口CN1集成?在引導

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> 

<application 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:roundIcon="@mipmap/ic_launcher_round" 
    android:supportsRtl="true" 
    android:theme="@style/AppTheme"> 
    <activity android:name=".MainActivity"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 

    <receiver 
     android:enabled="true" 
     android:exported="true" 
     android:name=".YourActivityRunOnStartup"> 

     <intent-filter> 
      <action android:name="android.intent.action.BOOT_COMPLETED" /> 
     </intent-filter> 
    </receiver> 
</application> 

回答

0

有一個關於提供功能選項的討論,如這些爲自動重新註冊但這需要這可能不是由所有所需的額外權限。

就我所知,最後一位在Android庫中要求使用本機代碼的人最後調用了這個代碼,但我不認爲他分享了他的代碼。

+0

好的我會使用本機代碼。我有關於地理圍欄方法的問題。大多數情況下,當應用程序在BG或刪除,調用onEntered()方法。但onExit()也被頻繁調用。 onExit方法的用途是什麼? – beck

+0

當您離開防護區時,它與onEnter相反 –

+0

我已經在本機android中解決了在啓動時自動重新註冊應用程序的問題,該應用程序在清單中具有接收器。我不太清楚在cn1中如何做到這一點? (PS。在代碼中動態註冊接收器無法正常工作)查看上面問題中的更新。 – beck