1

I have no clue how to solve this issue.
When I try to compile the folder to APK file, there is message that says

MainActivity.java uses or overrides a deprecated API.

I am using Android Studio version 3.2.0 and Java version 8. I tried everything but it doesn't work. I also searched up internet but I couldn't get the answer.

package com.kakao.talk.theme.apeach;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;

public class MainActivity extends Activity {
private static final String KAKAOTALK_SETTINGS_THEME_URI = 
"kakaotalk://settings/theme/";
private static final String MARKET_URI = "market://details?id=";
private static final String KAKAO_TALK_PACKAGE_NAME = "com.kakao.talk";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_activity);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        try {
            Window window = this.getWindow();
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.setStatusBarColor(getResources().getColor(R.color.statusBarColor));
        } catch (Throwable ignored) {
        }
    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        try {
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        } catch (Throwable ignored) {
        }
    }

    final Button applyButton = (Button) findViewById(R.id.apply);
    applyButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            final Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setData(Uri.parse(KAKAOTALK_SETTINGS_THEME_URI + getPackageName()));
            startActivity(intent);
            finish();
        }
    });

    final Button installButton = (Button) findViewById(R.id.market);
    installButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(MARKET_URI + KAKAO_TALK_PACKAGE_NAME));
            startActivity(intent);
            finish();
        }
    });

    try {
        getPackageManager().getPackageInfo(KAKAO_TALK_PACKAGE_NAME, 0);
        applyButton.setVisibility(View.VISIBLE);
        installButton.setVisibility(View.GONE);
    } catch (NameNotFoundException e) {
        applyButton.setVisibility(View.GONE);
        installButton.setVisibility(View.VISIBLE);
    }
}

}

Tamir Abutbul
  • 7,301
  • 7
  • 25
  • 53
user1222
  • 11
  • 1
  • 2
  • Hi usser1222, welcome to StackOverflow! Please note that APKs (should) still be compiled even if you get that log. (It should be a warning) Could you also try passing the `-Xlint:deprecation` flag to your Gradle command? – Edric Dec 27 '18 at 15:12
  • I changed your tagging for you, N.B. Java and JavaScript are two very, very different things. – Jared Smith Dec 27 '18 at 15:12
  • I ignored it and tried to download it onto my phone, but it does not work (for some reason...) What do you mean by "passing the Xlint:deprecation flag to Gradle command"? I am so sorry :( I am new to this whole stuff and I am lost *BTW thank you so much for changing the tag! – user1222 Dec 27 '18 at 15:13

1 Answers1

2

welcome to StackOverflow!

In your code, after the version check, you are trying to set the window status color by using getResources().getColor(), which has been deprecated for a few years now.

You can use the ContextCompat to achieve this: ContextCompat.getColor(context, R.color.color_name).

You should change R.color.color_name for your desired resource (in your case should be R.color.statusBarColor) as well as pass the correct context (which should be getApplicationContext()).

Further information can be found on this post, and never forget the great google's documentation on Android.

Renan Souza
  • 905
  • 9
  • 25
  • When I replaced getResources().getColor() with ContextCompat.getColor(context, R.color.color_name), I have errors reporting that they cannot find symbol variable color_name, etc... What should I do? – user1222 Dec 27 '18 at 15:24
  • The part "R.color.statusBarColor" worked like a charm, but "ContextCompat" and "getTargetContext()" does not work :( I am using 3.2.0 version of Android Studio.. Should I download the newest version just in case this one is outdated? – user1222 Dec 27 '18 at 15:35
  • No. It should work on 3.2.0. Are you importing the ContextCompat correctly? – Renan Souza Dec 27 '18 at 15:37
  • Your line should end-up on something like this: window.setStatusBarColor(ContextCompat.getColor(this.getApplicationContext(), R.color.statusBarColor) – Renan Souza Dec 27 '18 at 15:40
  • When I replace it with ContextCompat, it says "Compilation failed; see the compiler error output for details." – user1222 Dec 27 '18 at 15:41
  • Did you manage to make it work? If this answer is what you were looking for, please mark it as accepted. – Renan Souza Dec 27 '18 at 18:12
  • It's not working :( Do you happen to know the reason why it says "Compilation failed; see the compiler error output for details." when I replaced the wording to ContextCompat? – user1222 Dec 28 '18 at 01:16
  • You need to check on the console for further information regarding the error. – Renan Souza Dec 28 '18 at 12:42
  • How should I check the console? I am so sorry :( I am new to this whole system and I have no clue how to solve these issues.. – user1222 Dec 28 '18 at 14:52
  • I'm really sorry for being a little rude. But those other questions that surged, initially are not related to the question itself. The provided answer was tested and does work with different Android API levels, for further stuff regarding how the IDE works or other silly development doubts, per example, please refer to google or other stack overflow questions. – Renan Souza Jan 03 '19 at 17:53