package com.dk.demo;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class DemoActivity extends Activity {
public static final String ACTIVITYTAG="tag.of.activity";
public ChatService chatService;
private TextView msg;
private EditText xie ;
public ServiceConnection connection=new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
chatService = null;
System.out.println("Service disconnected to activity...");
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
chatService=((ChatService.ServiceBinder)service).getService();
System.out.println("Service connected to activity...");
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
msg=(TextView) findViewById(R.id.msg);
xie = (EditText)findViewById(R.id.xie);
Intent intent=new Intent(this, ChatService.class);
bindService(intent, connection, BIND_AUTO_CREATE);
registerBroadCast();
}
@Override
protected void onDestroy() {
super.onDestroy();
unbindService(connection);
}
public void send(View view){
Intent intent=new Intent(ChatService.SERVICETAG);
intent.putExtra("msg", xie.getText().toString());
sendBroadcast(intent);
}
private void registerBroadCast(){
ActivityBroadCast receiver =new ActivityBroadCast();
IntentFilter filter=new IntentFilter();
filter.addAction(ACTIVITYTAG);
registerReceiver(receiver, filter);
}
private class ActivityBroadCast extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(ACTIVITYTAG)){
Log.v("ActivityBroadCast", "onReceive");
String temp=intent.getStringExtra("resultmsg");
msg.setText(temp);
}
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
android简单的聊天demo
共31个文件
class:12个
png:6个
java:3个
3星 · 超过75%的资源 需积分: 23 111 下载量 101 浏览量
2013-01-29
13:34:34
上传
评论
收藏 55KB RAR 举报
温馨提示
主要就是一个简单的 服务和广播之间的 互相调用 如果开个服务器的话 应该是能够实现简单的聊天的功能的 哎 不熟悉 服务和广播的可以看看
资源推荐
资源详情
资源评论
收起资源包目录
Demo.rar (31个子文件)
Demo
.project 844B
project.properties 360B
proguard.cfg 1KB
src
com
dk
demo
ChatService.java 2KB
DemoActivity.java 2KB
AndroidManifest.xml 776B
res
drawable-ldpi
ic_launcher.png 2KB
drawable-hdpi
ic_launcher.png 4KB
drawable-mdpi
ic_launcher.png 3KB
layout
main.xml 733B
values
strings.xml 174B
assets
gen
com
dk
demo
R.java 781B
.classpath 364B
bin
resources.ap_ 11KB
classes
com
dk
demo
R$layout.class 373B
R$drawable.class 386B
R$attr.class 319B
ChatService.class 2KB
DemoActivity$ActivityBroadCast.class 1KB
R$id.class 387B
R.class 482B
R$string.class 406B
ChatService$ServiceBinder.class 582B
DemoActivity$1.class 1KB
ChatService$ServiceBroadCast.class 2KB
DemoActivity.class 3KB
res
drawable-ldpi
ic_launcher.png 2KB
drawable-hdpi
ic_launcher.png 4KB
drawable-mdpi
ic_launcher.png 2KB
classes.dex 6KB
Demo.apk 15KB
共 31 条
- 1
liaoshucai
- 粉丝: 0
- 资源: 22
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
前往页