android Service发送广播
来源:互联网 发布:win7 443端口服务 编辑:程序博客网 时间:2024/06/06 12:37
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
阅读全文
0 0
- android Service发送广播
- Android中Service发送广播给activity
- Android Activity接收Service发送的广播
- Android Activity接收Service发送的广播
- Android中用广播从Service中向Activity发送信息
- Android中用广播从Service中向Activity发送信息
- Android adb 发送广播、启动Activity、Service等
- Android中用广播从Service中向Activity发送信息
- Android Service和广播
- android 广播 service
- Android 发送广播
- android-发送自定义广播
- Android 发送广播
- Android发送UDP广播
- Android发送广播Broadcast
- Android发送本地广播
- Android 发送无序广播
- Android 发送自定义广播
- 记录C++学习bug历程
- Android7.0中文文档(API)-- Toolbar
- java集合类的一些内建函数分析
- 单链表习题(进阶二)——复杂链表复制
- 使用连接mysql的jdbc驱动最新版引发的问题
- android Service发送广播
- sqlite3用法
- window 下使用GNU工具的方法
- Android之Canvas.drawCircle()方法的理解
- TextView设置drawable
- MFC中 给按钮添加图片的方法
- kettle5.3源码部署
- 这些关于四轴飞行器名词你都明白么
- JS的一些小技巧