NotificationListerner服务在重新安装期间停止

时间:2018-08-01 15:20:12

标签: java android cordova cordova-plugins

我在Cordova平台上有一个NotificationListerner服务。

一切正常,但如果在调试过程中重新安装了该应用程序,则即使已为其启用了通知访问权限,该服务也会停止。

我必须手动将“通知访问”切换为“开”和“开”,然后才能恢复侦听器。这是为什么?这还可能意味着在生产期间实际更新可能会停止Listener服务?

LocalBroadcast.java

public class LocalBroadcast extends NotificationListenerService {
    Context context;
    private static final String TAG = LocalBroadcast.class.getSimpleName();    

    @Override
    public void onCreate() {    
        super.onCreate();
        Log.i(TAG, "onCreate");
        context = getApplicationContext(); // Is this the right way for a cordova plugin?    
    }

    @Override
    public IBinder onBind(Intent intent) {
        return super.onBind(intent);
    }

    @Override
    public void onNotificationPosted(StatusBarNotification sbn){

        String packageName = sbn.getPackageName();
        String ticker ="";
        if(sbn.getNotification().tickerText !=null) {
            ticker = sbn.getNotification().tickerText.toString();
        }
        Bundle extras = sbn.getNotification().extras;
        String title = extras.getString("android.title");
        String text = extras.getCharSequence("android.text").toString();

        Log.i(TAG,packageName);
        Log.i(TAG,ticker);
        Log.i(TAG,title);
        Log.i(TAG,text);

        Intent intent = new  Intent("com.arjun.localbroadcast");
        intent.putExtra("packageName", packageName);
        intent.putExtra("ticker", ticker);
        intent.putExtra("title", title);
        intent.putExtra("text", text);
        LocalBroadcastManager.getInstance(context).sendBroadcast(intent);

    }

    @Override
    public void onNotificationRemoved(StatusBarNotification sbn){

    }

}

NotificationAccess.java (MainActivity)

public class NotificationAccess extends CordovaPlugin {
  private static final String TAG = "NotificationAccess";

  // Init the broadcast manager 
  public void initialize(CordovaInterface cordova, CordovaWebView webView) {
    super.initialize(cordova, webView);
    LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(super.webView.getContext());
    lbm.registerReceiver(receiver, new IntentFilter("com.arjun.localbroadcast"));    
    Log.d(TAG, "Initialized");
    Log.d(TAG, lbm.toString());
  }

  // nothing here for now
  public boolean execute(String action, JSONArray args, final CallbackContext callbackContext) throws JSONException {
    if(action.equals("echo")) {
      String phrase = args.getString(0);
      Toast.makeText(webView.getContext(),phrase,Toast.LENGTH_LONG).show(); 
    }     
    return true;
  }

  // start receiving the broadcast
  public BroadcastReceiver receiver = new BroadcastReceiver() {
      @Override
      public void onReceive(Context context, Intent intent) {
          if (intent != null) {
              String packageName = intent.getStringExtra("packageName");
              Log.i(TAG,packageName);
              String title = intent.getStringExtra("title");
              Log.i(TAG,title);
          }
      }
  };

  @Override
  public void onDestroy() {
      super.onDestroy();
      LocalBroadcastManager.getInstance(super.webView.getContext()).unregisterReceiver(receiver);
      Log.i(TAG,"LOCAL BROADCAST KILLED");
  }
}

编辑- 哦,我在Android 6上

0 个答案:

没有答案