您的位置:首页 > 其它

Service分类,生命周期,普通方式启动

2016-06-27 14:52 441 查看
package com.example.lenovo.service;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

//普通方式启动
public void bt_1(View v)
{
//准备Intent:显式意图
Intent intent = new Intent(this,MyService.class);
intent.putExtra("test","发送的数据");
//启动Service
startService(intent);
Toast.makeText(MainActivity.this, "Service已启动", Toast.LENGTH_SHORT).show();
}
//普通方式关闭
public void bt_2(View v)
{
//准备Intent:显式意图
Intent intent = new Intent(this,MyService.class);
//关闭Service
stopService(intent);
Toast.makeText(MainActivity.this, "Service已停止", Toast.LENGTH_SHORT).show();
}


java
继承于Service,并在AndroidManifest中注册

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: