折腾:
【未解决】Android Studio中gradle编译rcjsta项目多个使用或覆盖了已过时的API等报错
期间,继续去解决编译错误:
> Task :tts:compileDebugJavaWithJavac 注: 某些输入文件使用或覆盖了已过时的 API。 注: 有关详细信息, 请使用 -Xlint:deprecation 重新编译。
如之前一样,去加上参数:
build.gradle
allprojects { repositories { // google() maven { url 'https://maven.aliyun.com/repository/google' } maven { url 'https://maven.aliyun.com/repository/public' } // jcenter() } gradle.projectsEvaluated { tasks.withType(JavaCompile) { // options.compilerArgs << "-Xlint:unchecked" options.compilerArgs << "-Xlint:deprecation" } } }
重新编译,看看具体是哪里报错了。
看到了,且compileDebugJavaWithJavac下面有2个文件,5个警告
先看第一个:
rcsjta/samples/api/tts/src/com/orangelabs/rcs/tts/PlayTextToSpeech.java
58: 警告: [deprecation] Service中的onStart(Intent,int)已过时 public void onStart(Intent intent, int startId) { ^
去看看
好像看不出啥
从源码的:
public class PlayTextToSpeech extends Service implements OnInitListener {
找到其被实现的类
android/app/Service.java
/** * @deprecated Implement {@link #onStartCommand(Intent, int, int)} instead. */ @Deprecated public void onStart(Intent intent, int startId) { }
onStart的确已过时,建议换:onStartCommand
/** * Called by the system every time a client explicitly starts the service by calling * {@link android.content.Context#startService}, providing the arguments it supplied and a * unique integer token representing the start request. Do not call this method directly. * * <p>For backwards compatibility, the default implementation calls * {@link #onStart} and returns either {@link #START_STICKY} * or {@link #START_STICKY_COMPATIBILITY}. * * <p>If you need your application to run on platform versions prior to API * level 5, you can use the following model to handle the older {@link #onStart} * callback in that case. The <code>handleCommand</code> method is implemented by * you as appropriate: * * {@sample development/samples/ApiDemos/src/com/example/android/apis/app/ForegroundService.java * start_compatibility} * * <p class="caution">Note that the system calls this on your * service's main thread. A service's main thread is the same * thread where UI operations take place for Activities running in the * same process. You should always avoid stalling the main * thread's event loop. When doing long-running operations, * network calls, or heavy disk I/O, you should kick off a new * thread, or use {@link android.os.AsyncTask}.</p> * * @param intent The Intent supplied to {@link android.content.Context#startService}, * as given. This may be null if the service is being restarted after * its process has gone away, and it had previously returned anything * except {@link #START_STICKY_COMPATIBILITY}. * @param flags Additional data about this start request. Currently either * 0, {@link #START_FLAG_REDELIVERY}, or {@link #START_FLAG_RETRY}. * @param startId A unique integer representing this specific request to * start. Use with {@link #stopSelfResult(int)}. * * @return The return value indicates what semantics the system should * use for the service's current started state. It may be one of the * constants associated with the {@link #START_CONTINUATION_MASK} bits. * * @see #stopSelfResult(int) */ public int onStartCommand(Intent intent, int flags, int startId) { onStart(intent, startId); return mStartCompatibility ? START_STICKY_COMPATIBILITY : START_STICKY; }
那就看看,如何换,毕竟好像多出一个参数,看看如何赋值
- onStart(Intent intent, int startId)
- onStartCommand(Intent intent, int flags, int startId)
多出个flags
默认都给0?
deprecation Service中的onStart(Intent,int)已过时
service onstart intent int deprecated
service onstart intent int deprecated onStartCommand
@Override public int onStartCommand(Intent intent, int flags, int startId) { showLog("onStartCommand is called"); //Service运行在UI主线程,为了避免因堵塞而被关闭,另开一个线程 new Thread(this).start(); return super.onStartCommand(intent, flags, startId); }
好像flags是传递进来的?
好像直接把上述的
@Override public void onStart(Intent intent, int startId) {
改为:onStartCommand
@Override // public void onStart(Intent intent, int startId) { public int onStartCommand(Intent intent, int flags, int startId) { Log.v(TAG, "Start TTS"); // Get parameters messages = intent.getStringArrayListExtra("messages"); // Instanciate the TTS engine try { tts = new TextToSpeech(getApplicationContext(), this); } catch (Exception e) { Log.v(TAG, "Can't instanciate TTS engine"); e.printStackTrace(); } return super.onStartCommand(intent, flags, startId); }
是不是就可以了?
去试试,重新编译,果然此处警告消失了。
【总结】
此处,Android中,之前实现Service,是要实现onStart
@Override public void onStart(Intent intent, int startId) {
现在:onStart已过时废弃
所以改为推荐的onStartCommand
整体改动,从:
@Override public void onStart(Intent intent, int startId) { Log.v(TAG, "Start TTS"); // Get parameters messages = intent.getStringArrayListExtra("messages"); // Instanciate the TTS engine try { tts = new TextToSpeech(getApplicationContext(), this); } catch (Exception e) { Log.v(TAG, "Can't instanciate TTS engine"); e.printStackTrace(); } }
改为:
@Override // public void onStart(Intent intent, int startId) { public int onStartCommand(Intent intent, int flags, int startId) { Log.v(TAG, "Start TTS"); // Get parameters messages = intent.getStringArrayListExtra("messages"); // Instanciate the TTS engine try { tts = new TextToSpeech(getApplicationContext(), this); } catch (Exception e) { Log.v(TAG, "Can't instanciate TTS engine"); e.printStackTrace(); } return super.onStartCommand(intent, flags, startId); }
即可。
主要改动:
- 把onStart换成onStartCommand
- 最后加上return返回
- return super.onStartCommand(intent, flags, startId);
转载请注明:在路上 » 【已解决】rcsjta项目编译出错:Task tts compileDebugJavaWithJavac 警告 deprecation Service中的onStart(Intent,int)已过时