android service fragment,Activity与Service通过Messenger实现自由通讯并将fragment加入Activity...

MainActivity.java文件如下:

import android.app.*;

import android.os.*;

import android.content.*;

import android.widget.*;

import android.view.*;

//主要思路如下:

//agirl接收来自服务的消息,同时aboys发送消息给服务

//sgirl接收来自活动的消息,同时sboys发送消息给活动

public class MainActivity extends Activity

{Messenger

aboys;//activity发送消息的messerger

Messenger

agirl;//activity接收消息的messenger

int

record;//用作信息的数字

@Override

protected

void onCreate(Bundle savedInstanceState)

{setContentView(R.layout.main);

super.onCreate(savedInstanceState);

Intent intent=new Intent();

intent.setClass(this,MainService.class);

startService(intent);

//启动服务

handler.sendEmptyMessageDelayed(1,3000);

//tt.beginTransaction().hide(pity).commit();

agirl=new Messenger(life);

record=0;

handler.postDelayed(new Runnable(){

public void run(){

life.sendEmptyMessage(1);

}

},10000);

}

//用于接收消息的handler,以此构建messenger

Handler life=new Handler(){

@Override

public void handleMessage(Message msg)

{record ;

setTitle(String.valueOf(record));

try

{

aboys.send(Message.obtain());//activity发送消息

}

catch (RemoteException e)

{setTitle("发生异常");}

super.handleMessage(msg);

}

};

Handler handler=new Handler(){

@Override

public void handleMessage(Message msg)

{pity pity=new pity();

FragmentController vv;

FragmentManager tt=getFragmentManager();

tt.beginTransaction().add(R.id.mainLinearLayout1,pity).commit();

//加入fragment

// TODO: Implement this method

super.handleMessage(msg);

}

};

class pity extends Fragment

{

@Override

public View onCreateView(LayoutInflater inflater, ViewGroup

container, Bundle savedInstanceState)

{

// TODO: Implement this method

return inflater.inflate(R.layout.dd,container,false);

}

@Override

public void onCreate(Bundle savedInstanceState)

{

connect();

//life.sendEmptyMessage(3);

//setContentView(R.layout.dd);

// TODO: Implement this method

super.onCreate(savedInstanceState);

}

}

//绑定服务使用

ServiceConnection sister=new ServiceConnection(){

@Override

public void onServiceConnected(ComponentName p1, IBinder p2)

{aboys=new Messenger(p2);

//利用传递过来的Ibinder实例化messenger

// TODO: Implement this method

}

@Override

public void onServiceDisconnected(ComponentName p1)

{

// TODO: Implement this method

}

};

public void connect(){

setTitle("Hi,I am here");

Intent intent=new Intent();

intent.setClass(this,MainService.class);

Bundle c=new Bundle();

c.putBinder("key",agirl.getBinder());

intent.putExtra("container",c);

bindService(intent,sister,BIND_IMPORTANT);

//绑定服务

}

}

MainService.java文件如下:

import android.app.*;

import android.content.*;

import android.os.*;

public class MainService extends Service

{int

record;//记录消息

Context

c;//上下文环境

Handler handler=new Handler(){

//此handler用于service接收消息

@Override

public void handleMessage(Message msg)

{try

{NotificationManager wm=(NotificationManager)

getSystemService(NOTIFICATION_SERVICE);

wm.notify(10,new Notification.Builder(c).setSmallIcon(R.drawable.ic_launcher).setContentText(String.valueOf(record)).build());

record ;

sboy.send(new

Message());//service发送消息

}

catch (RemoteException e)

{}

// TODO: Implement this method

super.handleMessage(msg);

}

};

Messenger sgirl;

Messenger sboy;

@Override

public IBinder onBind(Intent p1)

{record=0;

c=this;

sgirl=new Messenger(handler);

sboy=new

Messenger(p1.getBundleExtra("container").getBinder("key"));

// TODO: Implement this method

return

sgirl.getBinder();//返回接收消息的Ibinder

}

main.xml如下:

encoding="utf-8"?>

xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:gravity="center"

android:id="@ id/mainLinearLayout1">

android:text="@string/hello_world"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:background="#29C8E4"/>

dd.xml如下:

encoding="utf-8"?>

xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical"

android:background="#C8DB25">

AndroidManifest.xml文件如下:

encoding="utf-8"?>

xmlns:android="http://schemas.android.com/apk/res/android"

package="com.hong.app" >

android:allowBackup="true"

android:icon="@drawable/ic_launcher"

android:label="@string/app_name"

android:theme="@style/AppTheme"

android:resizeableActivity = "true">

android:name=".MainActivity"

android:label="@string/app_name" >

/>

android:name="android.intent.category.LAUNCHER"

/>

android:name=".MainService"

android:label="@string/app_name" >

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值