1.
package com.example.lsn.myapplication;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.content.IntentFilter;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.widget.Button;import android.widget.TextView;public class MainActivity extends AppCompatActivity {    Button startser;    Button stopser;    TextView textView;    Intent intent;    public static final String ACTION_UPDATEUI="updateUI";    UpdateUIBroadcastReceiver broadcastReceiver;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        intview();    }    private void intview() {        startser= (Button) findViewById(R.id.button);        stopser= (Button) findViewById(R.id.button6);        textView= (TextView) findViewById(R.id.textView);        broadcastReceiver=new UpdateUIBroadcastReceiver();        IntentFilter filter=new IntentFilter();        filter.addAction(ACTION_UPDATEUI);        registerReceiver(broadcastReceiver,filter);        startser.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                 intent=new Intent(MainActivity.this,Myser.class);                startService(intent);            }        });        stopser.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                stopService(intent);            }        });    }    private class UpdateUIBroadcastReceiver extends BroadcastReceiver {        @Override        public void onReceive(Context context, Intent intent) {            textView.setText("121231");            MyLog.showlog("rizhi", "onReceive");        }    }}
2.
package com.example.lsn.myapplication;import android.app.Service;import android.content.Intent;import android.os.IBinder;import android.support.annotation.IntDef;import android.support.annotation.Nullable;/** * Created by lsn on 2017/6/15. */public class Myser extends Service {    @Nullable    @Override    public IBinder onBind(Intent intent) {        MyLog.showlog("rizhi", "onStartCommand" + intent);        return null;    }    @Override    public void onCreate() {        super.onCreate();        MyLog.showlog("rizhi", "Service初始化");    }    @Override    public void onStart(Intent intent, int startId) {        super.onStart(intent, startId);        MyLog.showlog("rizhi", "Service初始化onStart");    }    @Override    public int onStartCommand(Intent intent, int flags, int startId) {        MyLog.showlog("rizhi", "onStartCommand");        MyLog.showlog("rizhi", "onStartCommand" + flags);        MyLog.showlog("rizhi", "onStartCommand" + startId);        Intent intent1 = new Intent();        intent1.setAction(MainActivity.ACTION_UPDATEUI);        sendBroadcast(intent1);        return super.onStartCommand(intent, flags, startId);    }    @Override    public void onDestroy() {        MyLog.showlog("rizhi", "onDestroy");        super.onDestroy();    }}
3.06-15 11:20:20.581 8572-8572/com.example.lsn.myapplication I/rizhi: Service初始化
06-15 11:20:20.581 8572-8572/com.example.lsn.myapplication I/rizhi: onStartCommand
06-15 11:20:20.581 8572-8572/com.example.lsn.myapplication I/rizhi: onStartCommand0
06-15 11:20:20.581 8572-8572/com.example.lsn.myapplication I/rizhi: onStartCommand1
06-15 11:20:20.581 8572-8572/com.example.lsn.myapplication I/rizhi: Service初始化onStart
06-15 11:20:20.601 8572-8572/com.example.lsn.myapplication I/rizhi: onReceive

更多相关文章

  1. IntentService实现下载
  2. android fragment动态显示隐藏
  3. android小项目demo2
  4. Android传感器之距离传感器
  5. Android(安卓)如何通过menu id来得到menu item 控件--binbinyang
  6. Android(安卓)实现适配器中的子控件与Activity通信
  7. 理解Android(安卓)Context
  8. android4.0 上定制状态栏
  9. Fresco源码解析 - 创建一个ImagePipeline(一)

随机推荐

  1. Android中的音频播放(MediaPlayer和SoundP
  2. Handler异步方式详解及其使用 .(转)
  3. DisplayMetrics获取手机屏幕大小(宽高)
  4. win7安装Android Studio
  5. android自带的功能
  6. Android(安卓)CPU Memory 资源测试和分析
  7. Android 让页面回滚到顶端
  8. android RadioGroup与TabHost组合使用达
  9. Android 蓝牙开发之一设置蓝牙
  10. android Thread 数据加载 ProgressDialog