intentservice 基本使用

intentservice代码

public class IntentServiceImpl extends IntentService {

    //注意构造方法需要无参(系统默认是有参数)
    //注意需要把super里面的name改为类型名
    public IntentServiceImpl() {
        super("IntentServiceImpl");
    }

    @Override
    protected void onHandleIntent(@Nullable Intent intent) {
        //可以处理耗时任务
        new Handler(Looper.getMainLooper()).post(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(IntentServiceImpl.this, "测试", Toast.LENGTH_SHORT).show();
            }
        });


    }
}

在mainfest注册  <service android:name=".IntentServiceImpl"/>

启动intentservice

Intent intent2 = new Intent(MainActivity.this, IntentServiceImpl.class);
intent2.putExtra("requestid", "t2");
startService(intent2);

猜你喜欢

转载自blog.csdn.net/qq_36237165/article/details/81282788