package com.bw.day15;
import androidx.appcompat.app.AppCompatActivity;
import android.Manifest;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//动态权限
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.M){
requestPermissions(new String[]{
Manifest.permission.INTERNET,
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE
},101);
}
MyReceiver myReceiver = new MyReceiver();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("com.bw.day15");
registerReceiver(myReceiver,intentFilter);
}
public void forqian(View view){
Intent intent = new Intent(this, MyService.class);
startService(intent);
}
public void intentservice(View view){
Intent intent = new Intent(this, MyIntentService.class);
Bundle bundle = new Bundle();
bundle.putString("url","http://vfx.mtime.cn/Video/2019/03/18/mp4/190318214226685784.mp4");
bundle.putString("path","/sdcard/Movies/cai.mp4");
intent.putExtras(bundle);
startService(intent);
}
public void getqing(View view){
Intent intent = new Intent(this, MyIntentService2.class);
Bundle bundle = new Bundle();
bundle.putString("url","http://43.143.146.165:7777/foods/getFoods?currentPage=1&pageSize=10");
bundle.putString("path","/sdcard/Movies/json.txt");
intent.putExtras(bundle);
startService(intent);
}
public void post(View view){
Intent intent = new Intent(this, MyIntentService3post.class);
Bundle bundle = new Bundle();
bundle.putString("urls","http://43.143.146.165:7777/foods/postFoods");
bundle.putString("params","pageSize=10¤tPage=1");
intent.putExtras(bundle);
startService(intent);
}
}
服务
package com.bw.day15;
import android.app.IntentService;
import android.app.Notification;
import android.content.Intent;
import android.content.Context;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.widget.RemoteViews;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class MyIntentService extends IntentService {
public MyIntentService() {
super("MyIntentService");
}
@Override
protected void onHandleIntent(Intent intent) {
Log.i("---service","handleInt");
Bundle extras = intent.getExtras();
String url = extras.getString("url");
String path = extras.getString("path");
try {
URL url1 = new URL(url);
HttpURLConnection httpURLConnection = (HttpURLConnection) url1.openConnection();
httpURLConnection.setRequestMethod("GET");
httpURLConnection.setConnectTimeout(2000);
httpURLConnection.setReadTimeout(2000);
httpURLConnection.connect();
if(httpURLConnection.getResponseCode()==200){
InputStream inputStream = httpURLConnection.getInputStream();
byte[] bytes = new byte[1024];
int len=0;
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
File file = new File(path);
FileOutputStream fileOutputStream = new FileOutputStream(file);
int max=httpURLConnection.getContentLength();
int progress=0;
while ((len=inputStream.read(bytes))!=-1){
fileOutputStream.write(bytes,0,len);
progress+=len;
dasas(max,progress);
}
Intent intent1 = new Intent();
intent1.setAction("com.bw.day15");
sendBroadcast(intent1);
}
}
} catch (MalformedURLException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private void dasas(int max, int progress) {
RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.item);
remoteViews.setProgressBar(R.id.pb,max,progress,false);
Notification.Builder builder = new Notification.Builder(this);
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setCustomContentView(remoteViews);
Notification build = builder.build();
startForeground(1,build);
}
}