关于Android9.0 此应用专为旧版Android打造,因此可能无法正常运行。请尝试检查更新或与开发者联系

最近发现之前开发的APP 在Android9.0上安装时提示如下恶心的提示:

关于Android9.0 此应用专为旧版Android打造,因此可能无法正常运行。请尝试检查更新或与开发者联系

度娘大部分技术是建议修改  android:targetSdkVersion="17"及以上版本

于是乎开始更新打包更新自己的APP,意外的事情发生了,闪退!!!  

debuger 发现如下错误:

关于Android9.0 此应用专为旧版Android打造,因此可能无法正常运行。请尝试检查更新或与开发者联系

低版本Androidsdk下之前正常,现在升级target报错,肯定跟这个有关,因为代码没有问题但总是报错。最后查阅资料了解到,在Android 4.0以上,网络连接不能放在主线程上,不然就会报错android.os.NetworkOnMainThreadException。但是4.0下版本可以不会报错。

贴出改造后代码:仅适合本项目使用哦,需要的拿走

     timer = new Timer(true);
     startTime = System.currentTimeMillis();

     timer.schedule(task, 2000, 2);
      

private  TimerTask task = new TimerTask() {
        @Override
        public void run() {
               if (task.scheduledExecutionTime() - startTime >= 1000 || !_touched) {
                                 Message message = new Message();   
                                 Log.i("SplashActivity", (task.scheduledExecutionTime() - startTime)+"");
                                 if(isConnectInternet()){
                                     TIME_UP=1;
                                     message.what = TIME_UP;
                                         
                                        Bundle data =  new  Bundle(); 
                                        HttpURLConnection connection = null;
                                        BufferedReader reader = null;
                                        try {
                                            URL url = new URL(url);
                                            connection = (HttpURLConnection) url.openConnection();
                                            connection.setRequestMethod("GET");
                                            connection.setConnectTimeout(5000);
                                            connection.setReadTimeout(5000);
                                            InputStream in = connection.getInputStream();
                                            //对获取到的输入流进行读取
                                            reader = new BufferedReader(new InputStreamReader(in,"UTF-8"));
                                            StringBuilder response = new StringBuilder();
                                            String line;
                                            while ((line = reader.readLine()) != null){
                                                response.append(line);
                                            }
                                            data.putString("value",response.toString()) ;
                                        } catch (Exception e) {
                                            e.printStackTrace();
                                        }finally {
                                            if (reader != null){
                                                try {
                                                    reader.close();
                                                } catch (IOException e) {
                                                    e.printStackTrace();
                                                }
                                            }
                                            if (connection != null){
                                                connection.disconnect();
                                            }
                                        }
                                        
                                        message.setData(data); 
                                         
                                  }else{
                                      message.what = 2;
                                  }                                
                                 handler.sendMessage(message);
                                 timer.cancel();
                                 this.cancel();
                              }

        }
    };

 

Handler handler =  new  Handler(){
        @Override    
        public void  handleMessage(Message msg) { 
             
            
             switch (msg.what) {   
                case 1:
                    Bundle data = msg.getData();    
                    String jsonStr = data.getString("value");
                    if (jsonStr.length() > 1) {
                        try {
                            jsonObject = new JSONObject(jsonStr.substring(1,
                                    jsonStr.length() - 1));
                           
                            linkUrl = jsonObject.getString("url")  ;

                          
                        } catch (JSONException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        

                        Intent intent = new Intent();
                        intent.setClass(MainActivity.this, WebviewActivity.class);
                        intent.putExtra("url", linkUrl);
                        intent.putExtra("type", "other");
                        startActivity(intent);
                        overridePendingTransition(R.anim.main_enter, R.anim.main_exit);
                        MainActivity.this.finish();
                     
            
                }
                      
                        break;
                case 2:
                    
                    
 
                    exit();
 
                    break;
             }   
             super.handleMessage(msg);  
        }
    
            
   
    };