Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
220 views
in Technique[技术] by (71.8m points)

android - NotificationListenerService not working after device BOOT_COMPLETE

I am working with NotificationListnerService it is working fine after some time it can not work and specially after device is boot it is not starting i have given all permissions which are required for listner here is my code.

public class Whatsapp_listner extends NotificationListenerService
{
 private static final String TAG = "NotificationListener";
 private static final String WA_PACKAGE = "com.whatsapp";

@Override
public void onListenerConnected() {
    super.onListenerConnected();
    Log.i("tag","Listner conneted");
}

@Override
public void onCreate() {
    super.onCreate();
}
@RequiresApi(api = Build.VERSION_CODES.Q)
@Override
public void onNotificationPosted(StatusBarNotification sbn) {
    if (!sbn.getPackageName().equals(WA_PACKAGE)) return;


    long yourmilliseconds = sbn.getPostTime();
    SimpleDateFormat sdf = new SimpleDateFormat("dd MMM yyyy");
    Date resultdate = new Date(yourmilliseconds);
    String date = sdf.format(resultdate);

    SimpleDateFormat my_time=new SimpleDateFormat("hh:mm aa");
    Date timer=new Date(yourmilliseconds);
    String time=my_time.format(timer);




    String modifiyedUniq = sbn.getUid() + sbn.getId() + sbn.getTag();


    Log.e(TAG, "Notification Key :: " + sbn.getKey());
    Log.e(TAG, "Notification Id :: " + sbn.getId());
    Log.e(TAG, "Notification postTime :: " + time);
    Log.e(TAG, "Notification From :: " + sbn.getPackageName());
    Log.e(TAG, "Notification TikerText :: " + sbn.getNotification().tickerText);
    Log.e(TAG, "Notification Title :: " + from);
    Log.e(TAG, "Notification Text :: " + message);


 

}

@RequiresApi(api = Build.VERSION_CODES.P)
@Override
public void onNotificationRemoved(StatusBarNotification sbn) {

    if (sbn.getPackageName().equals(WA_PACKAGE)){

        Log.i("notifyier", String.valueOf(sbn.getNotification().getSmallIcon().getResId()));
        Log.i("notifyier", String.valueOf(sbn.getNotification().extras.get("android.text")));
        int id=sbn.getNotification().getSmallIcon().getResId();
      

    }

}

This is working fine but after some time and after boot completes is does not work and i using broadcast receiver to start ListnerService but still it is not working

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I have found solution for this by just adding some lines of code

 @Override
public void onListenerConnected() {
    super.onListenerConnected();
    Log.i("tag","Listner conneted");
    tryReconnectService();
}


 public void tryReconnectService() {
    toggleNotificationListenerService();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        ComponentName componentName =
                new ComponentName(getApplicationContext(), Whatsapp_recorder.class);

        //It say to Notification Manager RE-BIND your service to listen notifications again inmediatelly!
        requestRebind(componentName);
    }
}

/**
 * Try deactivate/activate your component service
 */
private void toggleNotificationListenerService() {
    PackageManager pm = getPackageManager();
    pm.setComponentEnabledSetting(new ComponentName(this, Whatsapp_recorder.class),
            PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
    pm.setComponentEnabledSetting(new ComponentName(this, Whatsapp_recorder.class),
            PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...