Android Block Incoming SMS using BroadCastReceiver

前端 未结 1 1202
难免孤独
难免孤独 2021-01-27 10:05

I want to block incoming SMS message as long as my application is running . I could achieve that but the problem is after I close the app or even restart or even uninstall it th

相关标签:
1条回答
  • 2021-01-27 10:23

    Check if your app is running then only abort broadcast receiver else not

    //define this variable above onReceive() with default value as false;
    boolean appRunningInBack=false;
    
    ActivityManager am = (ActivityManager) mContext.getSystemService(Activity.ACTIVITY_SERVICE);
    String packageName = am.getRunningTasks(1).get(0).topActivity.getPackageName();
    
    if(packageName.equalIgnoreCase("your app package name") || appRunningInBack)
    {    
        appRunningInBack=true;
        abortBroadCast();
    } else {
    
    }
    

    Also add Permission in AndroidManifest.xml

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

    After this when your app goes in background.or while running .this variable will be true.

    0 讨论(0)
提交回复
热议问题