2

I am trying to call a method that is located in a Fragment from a class. Now I am getting the error: Non-static method checkWriteStoragePermission() cannot be referenced from a static content. I'm really out of ideas. I am googling for about 2 days now without any result.

The class where I am trying to call the method:

public class MyFirebaseMessagingService extends FirebaseMessagingService {

public static String sRecordDate;

public void onMessageReceived(RemoteMessage remoteMessage) {

    Intent intent = new Intent(this,MainActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_ONE_SHOT);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
    notificationBuilder.setContentTitle("FCM NOTIFICATION");
    notificationBuilder.setContentText(remoteMessage.getNotification().getBody());
    notificationBuilder.setAutoCancel(true);
    notificationBuilder.setSmallIcon(R.mipmap.ic_launcher);
    notificationBuilder.setContentIntent(pendingIntent);
    NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(0,notificationBuilder.build());

    for (Map.Entry<String, String> entry : remoteMessage.getData().entrySet()) {
        String key = entry.getKey();
        String value = entry.getValue();

        Log.d(":", "key, " + key + " value " + value);

        sRecordDate = value;

        RecordingMode.checkWriteStoragePermission(); //HERE I AM GETIING THE ERROR
    }
}

The method in the Fragment class:

    public void checkWriteStoragePermission() {
    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if(ContextCompat.checkSelfPermission(getActivity(), android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
                == PackageManager.PERMISSION_GRANTED) {
            mIsRecording = true;
            try {
                createVideoFileName();
            } catch (IOException e) {
                e.printStackTrace();
            }
            startRecord();
            mMediaRecorder.start();
            mChronometer.setBase(SystemClock.elapsedRealtime());
            mChronometer.setVisibility(View.VISIBLE);
            mChronometer.start();
        } else {
            if(shouldShowRequestPermissionRationale(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
                Toast.makeText(getActivity(), "app needs to be able to save videos", Toast.LENGTH_SHORT).show();
            }
            requestPermissions(new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_WRITE_EXTERNAL_STORAGE_PERMISSION_RESULT);
        }
    } else {
        mIsRecording = true;
        try {
            createVideoFileName();
        } catch (IOException e) {
            e.printStackTrace();
        }
        startRecord();
        mMediaRecorder.start();
        mChronometer.setBase(SystemClock.elapsedRealtime());
        mChronometer.setVisibility(View.VISIBLE);
        mChronometer.start();
    }
}

How can I fix this easily? As I cannot convert this into a static method.

Kind regards,

2 Answers2

4

But you are using this method as if it was static method (calling it by name of the class), create instance of the class where you have this method and call it on that instance. You said it's `Fragment' class, then:

Fragment fragment = new Fragment();
fragment.checkWriteStoragePermission();
Shadov
  • 5,421
  • 2
  • 19
  • 38
0

Your method checkWriteStoragePermission isn't static but you try to call it directly from your Service. Try editing the method like that:

public static void checkWriteStoragePermission() {
....
}
Lennart
  • 52
  • 3