后台操作及IntentService

IntentService

IntentService是继承自Service实现了异步功能的类。

Service是Android系统中的四大组件之一。

后台运行和跨进程访问。

Service可以在后台执行长时间运行操作

服务由其他应用组件启动,用户切换到其他应用,服务仍将在后台继续运行

IntentService-应用

第一步:新建一个MyService继承自IntentService

public class MyIntentService2 extends IntentService {
    public MyIntentService2() {
        super("MyIntentService2");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        
    }

}

第二步:开启服务

public void start(View view) {
        Intent intent=new Intent(this,MyforeproService.class);
        startService(intent);
    }
//在activity中开启服务

Service-前台服务

  1. 创建Service重写方法

publicclass MyServices extends Service {

private static final String TAG = "amy";

@Override

public void onCreate(){

super.onCreate();

Log.d(TAG,"onCreate");

}

  1. 启动服务

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.WRITE_EXTERNAL_STORAGE,
                    Manifest.permission.READ_EXTERNAL_STORAGE
            },101);
        }
    }

    public void start(View view) {
        Intent intent=new Intent(this,MyforeproService.class);
        startService(intent);
    }

    public void stop(View view) {
        Intent intent=new Intent(this,MyforeproService.class);
        stopService(intent);
    }

    public void staris(View view) {
        Intent intent=new Intent(this,MyIntentService.class);
        Bundle bundle=new Bundle();
        bundle.putString("u","http://vfx.mtime.cn/Video/2019/03/18/mp4/190318214226685784.mp4");
        bundle.putString("s","/sdcard/Movies/dog.mp4");
        intent.putExtras(bundle);
        startService(intent);
    }
}

Notification+IntentService

发送通知

public class MyIntentService extends IntentService {


    public MyIntentService() {
        super("MyIntentService");
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.i("---is","onCreate");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        Bundle bundle=intent.getExtras();
        String u=bundle.getString("u");
        String s=bundle.getString("s");
        try {
            URL url=new URL(u);
            HttpURLConnection httpURLConnection=(HttpURLConnection) url.openConnection();
            httpURLConnection.setRequestMethod("GET");
            httpURLConnection.setReadTimeout(2000);
            httpURLConnection.setConnectTimeout(2000);
            httpURLConnection.connect();
            if(httpURLConnection.getResponseCode()==200){
                InputStream inputStream=httpURLConnection.getInputStream();
                byte[] buffer=new byte[1024];
                File file=new File(s);
                int len=0;
                FileOutputStream fileOutputStream=new FileOutputStream(file);
                int max=httpURLConnection.getContentLength();
                int progress=0;
                while ((len=inputStream.read(buffer))!=-1){
                    fileOutputStream.write(buffer,0,len);
                    progress+=len;
                    sendno(max,progress);
                }
                    Intent intent1=new Intent();
                intent1.setAction("com.bw.ok");
                sendBroadcast(intent1);
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Log.i("---is","onHand");
    }
    public void sendno(int max,int progress){
        //发送通知
        NotificationManager notificationManager=(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        RemoteViews remoteViews=new RemoteViews(getPackageName(),R.layout.item);
        remoteViews.setProgressBar(R.id.pd,max,progress,false);
        Notification.Builder builder=new Notification.Builder(this);
        builder.setSmallIcon(R.mipmap.ic_launcher)
                .setCustomContentView(remoteViews);
        Notification notification=builder.build();
        notificationManager.notify(1,notification);
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.i("---is","onDestroy");
    }
}

public void staris(View view) {
        Intent intent=new Intent(this,MyIntentService.class);
        Bundle bundle=new Bundle();
        bundle.putString("u","http://vfx.mtime.cn/Video/2019/03/18/mp4/190318214226685784.mp4");
        bundle.putString("s","/sdcard/Movies/dog.mp4");
        intent.putExtras(bundle);
        startService(intent);
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值