失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > 此版本专旧版本为android 关于Android9.0 此应用专为旧版Android打造 因此可能无法

此版本专旧版本为android 关于Android9.0 此应用专为旧版Android打造 因此可能无法

时间:2022-02-28 15:12:31

相关推荐

此版本专旧版本为android 关于Android9.0 此应用专为旧版Android打造 因此可能无法

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

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

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

debuger 发现如下错误:

低版本Androidsdk下之前正常,现在升级target报错,肯定跟这个有关,因为代码没有问题但总是报错。最后查阅资料了解到,在Android 4.0以上,网络连接不能放在主线程上,不然就会报错workOnMainThreadException。但是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);

}

};

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

如果觉得《此版本专旧版本为android 关于Android9.0 此应用专为旧版Android打造 因此可能无法》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。