引文:

http://www.cnblogs.com/linjiqin/p/3147764.html

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".EX0315" > <TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:id="@+id/Ex0315TextView"
android:layout_marginLeft="20dp"
android:layout_marginTop="0dp"
android:text="@string/hello_world" /> <Button android:id="@+id/Ex0315StartButton"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="40dp"
android:text="@string/ex0315Startbutton" />
<Button android:id="@+id/Ex0315SendButton"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="110dp"
android:text="@string/ex0315Sendbutton" />
</RelativeLayout>
package com.iruisi.service;

import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log; public class Ex0315Service extends Service {
private static final String TAG = "MyService";
private Handler mHandler=new Handler();
private int mCount=;
private Runnable mRunnable = new Runnable() { @Override
public void run() {
mCount++;
sendMsg2Activity("start send to service");
Log.i("hippo","计算器"+Integer.toString(mCount));
mHandler.postDelayed(mRunnable,);
}
}; @Override
public void onCreate() {
super.onCreate();
Log.e(TAG, "start onCreate~~~");
//开启服务 延时1s
mHandler.postDelayed(mRunnable,);
} private void sendMsg2Activity(String msg){ //发送广播
Intent intent=new Intent();
intent.putExtra("count", mCount);
intent.setAction("com.iruisi.service.Ex0315Service");
sendBroadcast(intent);
}
@Override
public IBinder onBind(Intent intent) { return null;
}
@Override
public void onDestroy() {
mHandler.removeCallbacks(mRunnable);
super.onDestroy();
}
@Override
public boolean onUnbind(Intent intent) {
// TODO Auto-generated method stub
return super.onUnbind(intent);
}
@Override
public void onRebind(Intent intent) {
// TODO Auto-generated method stub
super.onRebind(intent);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
return super.onStartCommand(intent, flags, startId);
}
}
package com.example.hellowrold;

import com.example.hellowrold.R.id;
import com.iruisi.service.Ex0315Service; import android.R.string;
import android.os.Bundle;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView; public class EX0315 extends Activity { private TextView mTextView;
private Button startButton;
private Button sendbuButton;
private Ex0315ServiceReceiver receiver=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ex0315);
mTextView=(TextView)findViewById(id.Ex0315TextView);
startButton=(Button)findViewById(id.Ex0315StartButton);
startButton.setOnClickListener(new Button.OnClickListener() { @Override
public void onClick(View v) {
Intent sIntent=new Intent(EX0315.this,Ex0315Service.class);
//sIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startService(sIntent); receiver=new Ex0315ServiceReceiver();
IntentFilter mIntentFilter=new IntentFilter();
mIntentFilter.addAction("com.iruisi.service.Ex0315Service");
EX0315.this.registerReceiver(receiver, mIntentFilter);
}
});
sendbuButton=(Button)findViewById(id.Ex0315SendButton);
sendbuButton.setOnClickListener(new Button.OnClickListener() { @Override
public void onClick(View v) {
Intent sIntent=new Intent(EX0315.this,Ex0315Service.class);
stopService(sIntent);
}
});
mTextView.setText("hahh");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.ex0315, menu);
return true;
} public class Ex0315ServiceReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
Bundle mBundle=intent.getExtras();
int count=mBundle.getInt("count");
     //其实不用在广播里重新要获取一下TextView 对象 setText参数是字符串类型,不是整型
//mTextView=(TextView)findViewById(id.Ex0315TextView);
mTextView.setText(String.valueOf(count));
}
}
}
在manifest里添加

        <service
android:name="com.iruisi.service.Ex0315Service"
android:exported="false" >
</service>

 这个服务是在另外一个包里

点击发送信息到服务器按钮,其实是 "停止服务"命名错误,点击后将停止。

android 入门-Service实时向Activity通过BroadcastReceiver传递数据的更多相关文章

  1. 在Activity之间如何传递数据,请尽可能说出你所知道的传递数据的方法,并详细描述其实现过程。

    在Activity之间如何传递数据,请尽可能说出你所知道的传递数据的方法,并详细描述其实现过程. 答案:可以通过Intent对象.静态变量.剪切板和全局对象进行数据传递,具体的数据传递方法如下. 1. ...

  2. Android Service实时向Activity传递数据

    演示一个案例,需求如下:在Service组件中创建一个线程,该线程用来生产数值,每隔1秒数值自动加1,然后把更新后的数值在界面上实时显示. 步骤如下:1.新建一个android项目工程,取名为demo ...

  3. Service实时向Activity传递数据案例

    转自 http://www.cnblogs.com/linjiqin/p/3147764.html 演示一个案例,需求如下:在Service组件中创建一个线程,该线程用来生产数值,每隔1秒数值自动加1 ...

  4. android入门——Service

    简单记录一下四大组件之一的Service的简单实用. 先是最简单的用法,服务的开关,onBind方法的使用 package com.example.wkp.service; import androi ...

  5. android 入门-Service

    sdk 1.7 package com.example.hellowrold; import java.util.Random; import com.example.hellowrold.R.id; ...

  6. Android入门篇2-activity调用跟数据传递

    一.activity调用 假设ActivityTest调用SecondActivity 1.显示调用 button1.setOnClickListener(new View.OnClickListen ...

  7. Android Activity和Fragment传递数据

    1.Activity与Activity传递数据 UserLoginActivity.java: Intent welcomePage = new Intent(); Bundle dataBundle ...

  8. Android基础知识04—Activity活动之间传递数据

    ------活动之间传递数据------ 向下一个活动传递数据: Intent中提供了一系列的putExtra()方法,可以把数据暂存到Intent中,启动另一个活动的时候就可以取出来. 代码: (存 ...

  9. Android使用JNI实现Java与C之间传递数据(转)

    介绍Java如何将数据传递给C和C回调Java的方法.  java传递数据给C,在C代码中进行处理数据,处理完数据后返回给java.C的回调是Java传递数据给C,C需要用到Java中的某个方法,就需 ...

随机推荐

  1. 4. 如何解释dalvik字节码

    如何解释dalvik字节码 文档: 在Android系统源码目录dalvik\docs有相关指令文档 dalvik-bytecode.html 实战: 来直接实战模拟来理解枯燥的理论 用IDA打开一个 ...

  2. Python连接MySQL的准备工作

    首先要安装MySQL,64位的win7可以安装64或者32位的MySQL版本,安装之后,python需要一个工具才能连接MySQL,这个工具叫MySQL-python,去这里或者这里下载1.2.3版本 ...

  3. python查找并删除相同文件-UNIQ File-wxPython版本

    今天用wxPython做了一个GUI程序,我称之为UNIQ File,实现查找指定目录内的相同文件,主要原理是计算文件的md5值(计算前先找出文件大小相同的文件,然后计算这些文件的md5值,而不是所有 ...

  4. 3分钟,9个Q&A让你快速知道Docker到底是什么

    不论是Google.Amazon.Microsoft.VMware都纷纷拥戴,加入Docker和Container所掀起的新时代云端虚拟化行列,这两项技术成为了IT界的新趋势.Docker和Conta ...

  5. stty命令使用

    stty [ -a ] [ -g ] [ Options ]   stty(set tty)命令用于显示和修改当前注册的终端的属性. UNIX系统为键盘的输入和终端的输出提供了重要的控制手段,可以通过 ...

  6. (转)高性能网站架构之缓存篇—Redis集群搭建

    看过 高性能网站架构之缓存篇--Redis安装配置和高性能网站架构之缓存篇--Redis使用配置端口转发 这两篇文章的,相信你已经对redis有一定的了解,并能够安装上,进行简单的使用了,但是在咱们的 ...

  7. Java中使用Socket实现服务器端和客户端通讯

    创建服务器端serverSocket import java.io.BufferedReader; import java.io.IOException; import java.io.InputSt ...

  8. java web 学习 --第一天(Java三级考试)

    1.Servlet servlet是运行在web server或 application server端的Java程序,主要用于在服务器端产生动态内容. servlet 在服务器端主要有以下作用 读取 ...

  9. 1.JS设计模式-this,call&apply

    1. this,call&apply 1.1 this this是Javascript语言的一个关键字. 它代表函数运行时,自动生成的一个内部对象,只能在函数内部使用. 1.1.1 普通函数调 ...

  10. Django~Excel,PDF

    # Text file #response = HttpResponse(mimetype='text/plain')  #response['Content-Disposition'] = 'att ...