-
Notifications
You must be signed in to change notification settings - Fork 334
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2471 from WUJINH/master
- Loading branch information
Showing
31 changed files
with
726 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 112 additions & 0 deletions
112
...80902126/app/src/main/java/edu/hzuapps/androidlabs/soft1614080902126/FinallyActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
package edu.hzuapps.androidlabs.soft1614080902126; | ||
|
||
import android.content.Context; | ||
import android.content.Intent; | ||
import android.graphics.Bitmap; | ||
import android.graphics.BitmapFactory; | ||
import android.os.Handler; | ||
import android.os.Message; | ||
import android.support.v7.app.AppCompatActivity; | ||
import android.view.View; | ||
import android.widget.Button; | ||
import android.widget.ImageView; | ||
import android.os.*; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.net.HttpURLConnection; | ||
import java.net.MalformedURLException; | ||
import java.net.URL; | ||
|
||
|
||
public class FinallyActivity extends AppCompatActivity { | ||
private Handler handler=new Handler(); | ||
ImageView imageView; | ||
private String url = "https://image.baidu.com/search/down?tn=download&word=download&ie=utf8&fr=detail&url=https%3A%2F%2Ftimgsa.baidu.com%2Ftimg%3Fimage%26quality%3D80%26size%3Db9999_10000%26sec%3D1527672511486%26di%3Da8cc5e41d36523997cfb9b3c615b79f2%26imgtype%3D0%26src%3Dhttp%253A%252F%252Fpic28.nipic.com%252F20130417%252F12389388_002915416000_2.png&thumburl=https%3A%2F%2Fss0.bdstatic.com%2F70cFuHSh_Q1YnxGkpoWK1HF6hhy%2Fit%2Fu%3D529587146%2C4023250194%26fm%3D27%26gp%3D0.jpg"; | ||
Bitmap bmImg; | ||
|
||
//在消息队列中实现对控件的更改 | ||
private Handler handle = new Handler() { | ||
public void handleMessage(Message msg) { | ||
switch (msg.what) { | ||
case 0: | ||
System.out.println("111"); | ||
Bitmap bmp=(Bitmap)msg.obj; | ||
imageView.setImageBitmap(bmp); | ||
break; | ||
} | ||
}; | ||
}; | ||
|
||
@Override | ||
protected void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
setContentView(R.layout.activity_finally); | ||
imageView = (ImageView) findViewById(R.id.my_view); | ||
Button btn=(Button) findViewById(R.id.btn); | ||
btn.setOnClickListener(new View.OnClickListener() { | ||
|
||
@Override | ||
public void onClick(View v) { | ||
//新建线程加载图片信息,发送到消息队列中 | ||
new Thread(new Runnable() { | ||
|
||
@Override | ||
public void run() { | ||
// TODO Auto-generated method stub | ||
Bitmap bmp = getURLimage(url); | ||
Message msg = new Message(); | ||
msg.what = 0; | ||
msg.obj = bmp; | ||
System.out.println("000"); | ||
handle.sendMessage(msg); | ||
} | ||
}).start(); | ||
} | ||
}); | ||
} | ||
|
||
|
||
//加载图片 | ||
public Bitmap getURLimage(String url) { | ||
Bitmap bmp = null; | ||
try { | ||
URL myurl = new URL(url); | ||
// 获得连接 | ||
HttpURLConnection conn = (HttpURLConnection) myurl.openConnection(); | ||
conn.setConnectTimeout(6000);//设置超时 | ||
conn.setDoInput(true); | ||
conn.setUseCaches(false);//不缓存 | ||
conn.connect(); | ||
InputStream is = conn.getInputStream();//获得图片的数据流 | ||
bmp = BitmapFactory.decodeStream(is); | ||
is.close(); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
return bmp; | ||
} | ||
// | ||
// public Bitmap returnBitMap(String url) { | ||
// URL myFileUrl = null; | ||
// Bitmap bitmap = null; | ||
// try { | ||
// myFileUrl = new URL(url); | ||
// } catch (MalformedURLException e) { | ||
// e.printStackTrace(); | ||
// } | ||
// try { | ||
// HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection(); | ||
// conn.setDoInput(true); | ||
// conn.connect(); | ||
// InputStream is = conn.getInputStream(); | ||
// bitmap = BitmapFactory.decodeStream(is); | ||
// is.close(); | ||
// } catch (IOException e) { | ||
// e.printStackTrace(); | ||
// } | ||
// return bitmap; | ||
// } | ||
|
||
|
||
} |
67 changes: 67 additions & 0 deletions
67
...080902126/app/src/main/java/edu/hzuapps/androidlabs/soft1614080902126/InternetThread.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package edu.hzuapps.androidlabs.soft1614080902126; | ||
|
||
import android.support.v7.app.AppCompatActivity; | ||
import android.os.Bundle; | ||
import android.graphics.Bitmap; | ||
import android.graphics.BitmapFactory; | ||
import android.os.Environment; | ||
import android.os.Handler; | ||
import android.widget.ImageView; | ||
import android.content.Context; | ||
import android.os.Environment; | ||
import java.io.File; | ||
import java.io.FileOutputStream; | ||
import java.io.InputStream; | ||
import java.net.HttpURLConnection; | ||
import java.net.MalformedURLException; | ||
import java.net.URL; | ||
|
||
|
||
public class InternetThread extends Thread { | ||
private String url; | ||
private ImageView imageView; | ||
private Handler handler; | ||
public InternetThread(String url, ImageView imageView, Handler handler){ | ||
this.url=url; | ||
this.imageView=imageView; | ||
this.handler=handler; | ||
} | ||
@Override | ||
public void run(){ | ||
try { | ||
URL httpUrl=new URL(url); | ||
HttpURLConnection conn=(HttpURLConnection)httpUrl.openConnection(); | ||
conn.setReadTimeout(5000); | ||
conn.setRequestMethod("GET"); | ||
conn.setDoInput(true); | ||
InputStream in=conn.getInputStream(); | ||
FileOutputStream out=null; | ||
File downloadFile=null; | ||
String fileName=String.valueOf(System.currentTimeMillis()); | ||
String storePath = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "demo"; | ||
File appDir = new File(storePath); | ||
|
||
if (!appDir.exists()) { | ||
appDir.mkdir(); } | ||
downloadFile=new File(appDir,fileName); | ||
out=new FileOutputStream(downloadFile); | ||
|
||
byte [] b=new byte[4*1024]; | ||
int len; | ||
if(out!=null) { | ||
while ((len = in.read(b)) != -1) { | ||
out.write(b,0,len); | ||
} | ||
} | ||
final Bitmap bitmap= BitmapFactory.decodeFile(downloadFile.getAbsolutePath()); | ||
handler.post(new Runnable() { | ||
@Override | ||
public void run() { | ||
imageView.setImageBitmap(bitmap); | ||
} | ||
}); | ||
} catch (java.io.IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} |
109 changes: 109 additions & 0 deletions
109
...080902126/app/src/main/java/edu/hzuapps/androidlabs/soft1614080902126/SecondActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package edu.hzuapps.androidlabs.soft1614080902126; | ||
import android.content.Intent; | ||
import android.os.Bundle; | ||
import android.os.Handler; | ||
import android.os.Message; | ||
import android.app.Activity; | ||
import android.support.v7.app.AppCompatActivity; | ||
import android.view.Menu; | ||
import android.view.View; | ||
import android.view.View.OnClickListener; | ||
import android.widget.Button; | ||
import android.widget.TextView; | ||
|
||
public class SecondActivity extends Activity { | ||
private TextView mint; | ||
private TextView sec; | ||
private Button start; | ||
private Button reset; | ||
private long timeusedinsec; | ||
private boolean isstop = false; | ||
private Handler mHandler = new Handler() { | ||
/* | ||
* edit by yuanjingchao 2014-08-04 19:10 | ||
*/ | ||
@Override | ||
public void handleMessage(Message msg) { | ||
// TODO Auto-generated method stub | ||
super.handleMessage(msg); | ||
switch (msg.what) { | ||
case 1: | ||
// 添加更新ui的代码 | ||
if (!isstop) { | ||
updateView(); | ||
mHandler.sendEmptyMessageDelayed(1, 1000); | ||
} | ||
break; | ||
case 0: | ||
break; | ||
} | ||
} | ||
|
||
}; | ||
|
||
@Override | ||
protected void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
setContentView(R.layout.activity_second); | ||
initViews(); | ||
|
||
Button button2=(Button)findViewById(R.id.button_2); | ||
button2.setOnClickListener(new View.OnClickListener(){ | ||
|
||
public void onClick(View v){ | ||
Intent intent=new Intent(SecondActivity.this,ThirdActivity.class); | ||
startActivity(intent); | ||
} | ||
}); | ||
} | ||
private void initViews() { | ||
mint = (TextView) findViewById(R.id.mint); | ||
sec = (TextView) findViewById(R.id.sec); | ||
reset = (Button) findViewById(R.id.reset); | ||
start = (Button) findViewById(R.id.start); | ||
reset.setOnClickListener(new OnClickListener() { | ||
@Override | ||
public void onClick(View arg0) { | ||
// TODO Auto-generated method stub | ||
|
||
mint.setText("00"); | ||
sec.setText("00"); | ||
start.setText("start"); | ||
timeusedinsec=0; | ||
isstop=true; | ||
} | ||
}); | ||
start.setOnClickListener(new OnClickListener() { | ||
@Override | ||
public void onClick(View arg0) { | ||
// TODO Auto-generated method stub | ||
mHandler.removeMessages(1); | ||
String aaa=start.getText().toString(); | ||
if(aaa.equals("开始学习")){ | ||
mHandler.sendEmptyMessage(1); | ||
isstop = false; | ||
start.setText("暂停"); | ||
}else { | ||
mHandler.sendEmptyMessage(0); | ||
isstop = true; | ||
start.setText("开始学习"); | ||
} | ||
|
||
} | ||
}); | ||
} | ||
private void updateView() { | ||
timeusedinsec += 1; | ||
int minute = (int) (timeusedinsec / 60)%60; | ||
int second = (int) (timeusedinsec % 60); | ||
if (minute < 10) | ||
mint.setText("0" + minute); | ||
else | ||
mint.setText("" + minute); | ||
if (second < 10) | ||
sec.setText("0" + second); | ||
else | ||
sec.setText("" + second); | ||
} | ||
|
||
} |
29 changes: 27 additions & 2 deletions
29
...pp/src/main/java/edu/hzuapps/androidlabs/soft1614080902126/Soft1614080902126Activity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,30 @@ | ||
package edu.hzuapps.androidlabs.soft1614080902126; | ||
|
||
public class Soft1614080902126Activity{ | ||
import android.content.Intent; | ||
import android.os.Bundle; | ||
import android.support.v7.app.AppCompatActivity; | ||
import android.view.View; | ||
import android.widget.Button; | ||
|
||
} | ||
public class Soft1614080902126Activity extends AppCompatActivity{ | ||
|
||
@Override | ||
protected void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
setContentView(R.layout.activity_soft1614080902126); | ||
Button button1=(Button)findViewById(R.id.button_1); | ||
button1.setOnClickListener(new View.OnClickListener(){ | ||
|
||
public void onClick(View v){ | ||
Intent intent=new Intent(Soft1614080902126Activity.this,SecondActivity.class); | ||
startActivity(intent); | ||
} | ||
}); | ||
|
||
|
||
|
||
} | ||
|
||
|
||
|
||
} |
Oops, something went wrong.