Detect or notify app notification settings Changed
Output:
Log:
09-25 05:39:01.512 17742-17742/com.pyarinc.deviceinfodemo D/SettingsChangesActivity: DeviceInformation settings changed!
09-25 05:39:02.551 17742-17742/com.pyarinc.deviceinfodemo D/SettingsChangesActivity: DeviceInformation settings changed!
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.pyarinc.deviceinfodemo">
<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">
</activity>
<activity android:name=".AccessNotificationActivity">
</activity>
<activity android:name=".SettingsChangesActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<service
android:name=".service.DetectAppNotificationService"
android:enabled="true"
android:exported="true">
</service>
</application>
</manifest>
SettingsChangesActivity.java
public class SettingsChangesActivity extends AppCompatActivity {
private SettingsContentObserver mSettingsContentObserver;
private DetectAppNotificationService mDetectAppNotificationService;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings_changes);
Intent intent = new Intent(this, DetectAppNotificationService.class);
startService(intent);
bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
}
public void startListener(View view) {
new CountDownTimer(Integer.MAX_VALUE, 1000) {
@Override
public void onTick(long l) {
/*Log.d("SettingsChangesActivity",""+mDetectAppNotificationService.checkNotificationIsEnabled());*/
}
@Override
public void onFinish() {
}
}.start();
}
@Override
protected void onStart() {
super.onStart();
Intent intent = new Intent(this, DetectAppNotificationService.class);
startService(intent);
}
@Override
protected void onDestroy() {
super.onDestroy();
Intent intent = new Intent(this, DetectAppNotificationService.class);
startService(intent);
unbindService(mConnection);
}
private ServiceConnection mConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
/* DetectAppNotificationService.NotificationBinder binder = (DetectAppNotificationService.NotificationBinder) iBinder;
mDetectAppNotificationService =binder.getService();*/
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
}
};
}
DetectAppNotificationService.java
public class DetectAppNotificationService extends Service {
public IBinder mIBinder = new NotificationBinder();
public Context mContext;
private boolean mPushNotifiChanged;
private Singlton mSinglton;
public static final String TAG = "SettingsChangesActivity";
private boolean mPushNotificationValue;
public DetectAppNotificationService() {
}
public DetectAppNotificationService(Context context) {
mContext = context;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
mSinglton = Singlton.getInstance();
callIndefinately();
return START_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
return mIBinder;
}
public class NotificationBinder extends Binder {
public DetectAppNotificationService getService() {
return DetectAppNotificationService.this;
}
}
public boolean checkNotificationIsEnabled() {
return NotificationManagerCompat.from(getApplicationContext()).areNotificationsEnabled();
}
public void callIndefinately() {
new CountDownTimer(Integer.MAX_VALUE, 1000) {
@Override
public void onTick(long l) {
/*Log.d("SettingsChangesActivity",""+checkNotificationIsEnabled()+" "+
checkNotificationIsEnabled());*/
mPushNotifiChanged = checkNotificationIsEnabled();
/*Singlton.l(TAG,"Sendig::"+" "+mPushNotifiChanged+" Is changed ::"+mIsStatusChanged(mPushNotifiChanged)+" after mPushNotificationValue ::"+mSinglton.mPushNotificationValue);*/
if (mIsStatusChanged(mPushNotifiChanged)) {
Toast.makeText(getApplicationContext(), "DeviceInformation settings changed!", Toast.LENGTH_SHORT).show();
Singlton.l(TAG,"DeviceInformation settings changed!");
}
}
@Override
public void onFinish() {
}
}.start();
}
public boolean mIsStatusChanged(boolean mPushNotificationValue) {
boolean returnValue ;
if (this.mPushNotificationValue == mPushNotificationValue) {
returnValue = true;
this.mPushNotificationValue = !mPushNotificationValue;
} else {
returnValue = false;
}
return returnValue;
}
}