android 入门-Service实时向Activity通过BroadcastReceiver传递数据
引文:
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传递数据的更多相关文章
- 在Activity之间如何传递数据,请尽可能说出你所知道的传递数据的方法,并详细描述其实现过程。
在Activity之间如何传递数据,请尽可能说出你所知道的传递数据的方法,并详细描述其实现过程. 答案:可以通过Intent对象.静态变量.剪切板和全局对象进行数据传递,具体的数据传递方法如下. 1. ...
- Android Service实时向Activity传递数据
演示一个案例,需求如下:在Service组件中创建一个线程,该线程用来生产数值,每隔1秒数值自动加1,然后把更新后的数值在界面上实时显示. 步骤如下:1.新建一个android项目工程,取名为demo ...
- Service实时向Activity传递数据案例
转自 http://www.cnblogs.com/linjiqin/p/3147764.html 演示一个案例,需求如下:在Service组件中创建一个线程,该线程用来生产数值,每隔1秒数值自动加1 ...
- android入门——Service
简单记录一下四大组件之一的Service的简单实用. 先是最简单的用法,服务的开关,onBind方法的使用 package com.example.wkp.service; import androi ...
- android 入门-Service
sdk 1.7 package com.example.hellowrold; import java.util.Random; import com.example.hellowrold.R.id; ...
- Android入门篇2-activity调用跟数据传递
一.activity调用 假设ActivityTest调用SecondActivity 1.显示调用 button1.setOnClickListener(new View.OnClickListen ...
- Android Activity和Fragment传递数据
1.Activity与Activity传递数据 UserLoginActivity.java: Intent welcomePage = new Intent(); Bundle dataBundle ...
- Android基础知识04—Activity活动之间传递数据
------活动之间传递数据------ 向下一个活动传递数据: Intent中提供了一系列的putExtra()方法,可以把数据暂存到Intent中,启动另一个活动的时候就可以取出来. 代码: (存 ...
- Android使用JNI实现Java与C之间传递数据(转)
介绍Java如何将数据传递给C和C回调Java的方法. java传递数据给C,在C代码中进行处理数据,处理完数据后返回给java.C的回调是Java传递数据给C,C需要用到Java中的某个方法,就需 ...
随机推荐
- linux 终端全局代理设置
http://www.webupd8.org/2010/10/how-to-set-proxy-for-terminal-quick.html 即 export http_proxy='http:// ...
- ndk学习18: JNI之C&C++调用Java
一.Java反射机制 先了解Java反射机制原理 例子网上很多,反射很灵活 二. 在JNI层调Java 用途: Java层逆向比较容易,增加逆向门槛,把调用都写到JNI层, 让Java层无调用关系 ...
- Android俄罗斯方块AI设计文档
首先上源码: https://github.com/bingghost/SimpleTetris 一.概要 使用了2种AI算法: 一种是经典的Pierre Dellacherie算法 一种基于基于深度 ...
- 页面遮罩层,并且阻止页面body滚动。bootstrap模态框原理
实现思路: 1.需要有一个层将body遮住,放在body上方. 2.修改body的overflow属性值为:hidden 废话不多说了,将关键代码贴出来了,兼容火狐,谷歌,ie 遮罩层的样式代码,红色 ...
- JQuery 遍历 - prev() 方法
http://www.w3school.com.cn/jquery/traversing_prev.asp http://www.w3school.com.cn/jquery/jquery_ref_t ...
- sharepoint2013隐藏左侧导航栏更换新的
$("#zz16_V4QuickLaunchMenu").hide()//隐藏 更换新的 <script type="text/javascript" s ...
- poj 1521
http://poj.org/problem?id=1521 题意:给你一个字符串,首先是计算出一个按正常编码的编码长度,其次是计算出一个用霍夫曼编码的编码长度,最后求正常编码的长度除以霍夫曼编码长度 ...
- iOS keyChain 的使用
详细资料,请参看苹果官方文档Keychain Services Reference . ios中的keychain,用于保存用户的机密信息,对keychain的操作有4种,就是 增,删,改,查: Se ...
- 使用phpmyadmin修改XAMPP中MySQL的默认空密码
XAMPP是开发php应用的一套完整的工具合集,就像安装软件一样安装,其他的都配置好了,不用自己再去繁琐的单独配置Apache.MySQL.php这几个模块了,以前我一直在使用的是Appserv,也是 ...
- Linux内存管理原理
本文以32位机器为准,串讲一些内存管理的知识点. 1. 虚拟地址.物理地址.逻辑地址.线性地址 虚拟地址又叫线性地址.linux没有采用分段机制,所以逻辑地址和虚拟地址(线性地址)(在用户态,内核态逻 ...