У меня есть код Java, который переводит устройство в режим «Не беспокоить». Как я могу использовать этот код из React-Native. Я узнаю о Native Modules в документации, но не знаю, как я могу вызвать это из реакции native.
package com.toggle_airplane_mode;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.AudioManager;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;
import android.widget.CompoundButton;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.SwitchCompat;
import static android.app.NotificationManager.ACTION_NOTIFICATION_POLICY_ACCESS_GRANTED_CHANGED;
public class MainActivity extends AppCompatActivity {
private NotificationManager mNotificationManager;
private boolean hasAccess = false;
private NotificationPolicyAccessChangedReceiver broadcastReceiver;
private SwitchCompat switch_;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
hasAccess = mNotificationManager.isNotificationPolicyAccessGranted();
registerBroadcast();
if(!hasAccess) checkIsNotificationPolicyAccessGranted();
switch_.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
changeDndMode(isChecked);
}
});
} else {
switch_.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
disableSounds(isChecked);
}
});
}
}
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(broadcastReceiver);
}
@RequiresApi(Build.VERSION_CODES.M)
private void registerBroadcast() {
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(ACTION_NOTIFICATION_POLICY_ACCESS_GRANTED_CHANGED);
broadcastReceiver = new NotificationPolicyAccessChangedReceiver(new Runnable() {
@Override
public void run() {
hasAccess = mNotificationManager.isNotificationPolicyAccessGranted();
Log.d("myLog", "has access updated to $hasAccess");
}
});
registerReceiver(broadcastReceiver, intentFilter);
}
@RequiresApi(Build.VERSION_CODES.M)
private void checkIsNotificationPolicyAccessGranted() {
if (!mNotificationManager.isNotificationPolicyAccessGranted()) {
Intent intent = new Intent(Settings.ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS);
startActivity(intent);
}
}
@RequiresApi(Build.VERSION_CODES.M)
private void changeDndMode(boolean b) {
if (hasAccess) mNotificationManager.setInterruptionFilter(
b
? NotificationManager.INTERRUPTION_FILTER_NONE
: NotificationManager.INTERRUPTION_FILTER_ALL
);
}
private void disableSounds(boolean b) {
AudioManager am = (AudioManager) getBaseContext().getSystemService(Context.AUDIO_SERVICE)
am.setRingerMode(
b
? AudioManager.RINGER_MODE_SILENT
: AudioManager.RINGER_MODE_NORMAL
);
}
}
class NotificationPolicyAccessChangedReceiver extends BroadcastReceiver {
private Runnable r;
public NotificationPolicyAccessChangedReceiver(Runnable r) {
this.r = r;
}
@Override
public void onReceive(Context context, Intent intent) {
if(intent != null) {
if(ACTION_NOTIFICATION_POLICY_ACCESS_GRANTED_CHANGED.equals(intent.getAction())) {
Log.d("myLog", "necessary broadcast receiver caught");
r.run();
}
}
}
}
Я создаю два класса, таких как SilenceMode.java и SilencePackage.java, и, если пытаюсь вставить этот код вSilenceMode.java это не работает.