android调用activity方法吗,Activity调用服务里的方法

首先写个类继承Service 在清单文件中注册

Activity与服务连接 重写onBinder 方法

···

···

布局文件xml

···

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

tools:context="{relativePackage}.{activityClass}" >

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="bind开启服务"

android:onClick="start"

/>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="bind关闭服务"

android:onClick="stop"

/>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="调用服务的方法"

android:onClick="method"

/>

···

*************MainActivity中*************

···

package com.example.test23_bindsecevice;

import com.example.test23_bindsecevice.BindService.MyBinder;

import android.app.Activity;

import android.content.ComponentName;

import android.content.Intent;

import android.content.ServiceConnection;

import android.os.Bundle;

import android.os.IBinder;

import android.util.Log;

import android.view.View;

public class MainActivity extends Activity {

private MyConnection conn;

MyBinder myBinder;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

}

@Override

protected void onDestroy() {

super.onDestroy();

//把service关闭 解除跟当前Activiy的绑定

unbindService(conn);

}

public void start(View v){

Intent service = new Intent(this,BindService.class);

//通过绑定的方式开启service

conn = new MyConnection();

//使用bindService 开启反服务

//参数 1. intent 包含要启动的service

//2. ServiceConnection接口 通过它可以接受服务开启或者停止的消息

//3.开启服务时操作的选项 一般传入BIND_AUTO_CREATE自动创建service

bindService(service, conn, BIND_AUTO_CREATE);//绑定存在自动创建

}

public void stop(View v){

//使用bindService开启服务 要是用unbindService停止

unbindService(conn);

}

public void method(View v){

//BindService service = new BindService();

// service.showToast("12112");

myBinder.method("dfdg");

myBinder.showToast2("4512") ;

}

private class MyConnection implements ServiceConnection{

@Override//当服务与Activity 连接时建立

public void onServiceConnected(ComponentName name, IBinder service) {

//只有当service onbind方法返回值不为null 调用

Log.e("TAG", "onServiceConnected");

myBinder=(MyBinder) service;

}

@Override

public void onServiceDisconnected(ComponentName name) {

Log.e("TAG", "onServiceDisconnected");

}

}

}

···

Service中*********************

···

package com.example.test23_bindsecevice;

import android.app.Service;

import android.content.Intent;

import android.os.Binder;

import android.os.IBinder;

import android.util.Log;

import android.widget.Toast;

public class BindService extends Service{

@Override

public IBinder onBind(Intent intent) {

Log.e("TAG", "onBind");

return new MyBinder();

}

@Override

public void onCreate() {

Log.e("TAG", "onBind");

super.onCreate();

}

@Override

public int onStartCommand(Intent intent, int flags, int startId) {

Log.e("TAG", "onStartCommand");

return super.onStartCommand(intent, flags, startId);

}

@Override

public void onDestroy() {

Log.e("TAG", "onDestroy");

super.onDestroy();

}

public void showToast(String s){

Toast.makeText(getApplicationContext(), s, Toast.LENGTH_SHORT).show();

}

public class MyBinder extends Binder{

public void method(String s){

showToast(s);

}

public void showToast2(String s){

Toast.makeText(getApplicationContext(), s, Toast.LENGTH_SHORT).show();

}

}

}

···

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值