您的位置:首页 > 移动开发 > Android开发

学习Android中服务总结(2)

2017-02-13 11:02 281 查看
Services远程服务

多个应用程序公用一个Services 我在这样做一个登陆功能 使2个应用程序公用一个Services布局文件如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main"
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"
android:orientation="vertical"
tools:context="com.example.g150831_android_qq.MainActivity">

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="10000"
android:id="@+id/et_main_number"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="123456"
android:id="@+id/et_main_pwd"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登陆"
android:onClick="login"
/>
</LinearLayout>

新建一个接口 里面写一个登陆判断的方法:

public interface QQLoginInterface {
public boolean login(String number,String pwd);
}

在新建一个AIDL文件 要注意的是文件名不能重复因为新建后它会自动创建一个同名的java文件 新建后记得Build一下:

interface com.example.g150831_android_qq.QQLoginInterfaceOut {
boolean login(String number,String pwd);
}

写一个服务 继承于Service 重写里面的onBind()方法 返回一个Bind 所以你要自己写一个Bind 继承于Bind并实现接口让后在重写里面的判断方法:

public class QQLoginService extends Service{
class MyIBinder extends Binder implements QQLoginInterface{

@Override
public boolean login(String number, String pwd)  {
if("10000".equals(number)&&"123456".equals(pwd)){
return true;
}
return false;
}
}

@Nullable
@Override
public IBinder onBind(Intent intent) {
Log.i("test","onBind");
return new MyIBinder();
}

}

在MainActivity里面重写onResume()方法 在onResume()里面绑定服务

public class MainActivity extends AppCompatActivity {

private EditText et_main_number;
private EditText et_main_pwd;
private Intent intent;
private QQLoginService.MyIBinder myIBinder;
private QQLoginInterface qqLoginInterface;
private QQLoginInterfaceOut qqLoginInterfaceOut;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_main_number = (EditText) findViewById(R.id.et_main_number);
et_main_pwd = (EditText) findViewById(R.id.et_main_pwd);
intent = new Intent(this,QQLoginService.class);
}
ServiceConnection connection=new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
Log.i("test","绑定成功");
qqLoginInterfaceOut = QQLoginInterfaceOut.Stub.asInterface(iBinder);
}

@Override
public void onServiceDisconnected(ComponentName componentName) {

}
};

@Override
protected void onResume() {
super.onResume();
bindService(intent,connection, Service.BIND_AUTO_CREATE);
}

public void login(View view){
String number=et_main_number.getText().toString();
String pwd=et_main_pwd.getText().toString();
boolean flag= false;
try {
flag = qqLoginInterfaceOut.login(number,pwd);
} catch (RemoteException e) {
e.printStackTrace();
}
Toast.makeText(this, ""+flag, Toast.LENGTH_SHORT).show();
}
}
在清单文件中配置一下:

 <service android:name=".QQLoginService" android:exported="true" ></service>

最后在新建一个项目 把上一个AIDL文件复制到这个项目上 记住把包也要复制过来 布局文件 跟MainActivity基本一样 Android 5.0 之后,启动其他应用程序的服务,不允许使用式

 ComponentName componentName=new ComponentName("cn.veryedu.g0305_android18_services","cn.veryedu.g0305_android18_services.MyServices");

 intent.setComponent(componentName);

布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main"
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"
android:orientation="vertical"
tools:context="com.example.g150831_android_wx.MainActivity">

<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="VXlogin"
android:textSize="30sp"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="10000"
android:id="@+id/et_main_number"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="123456"
android:id="@+id/et_main_pwd"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登陆"
android:onClick="login"
/>
</LinearLayout>

MainActivity:

package com.example.g150831_android_wx;

import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.example.g150831_android_qq.QQLoginInterfaceOut;

public class MainActivity extends AppCompatActivity {

private EditText et_main_number;
private EditText et_main_pwd;
private Intent intent;
private QQLoginInterfaceOut qqLoginInterfaceOut;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_main_number = (EditText) findViewById(R.id.et_main_number);
et_main_pwd = (EditText) findViewById(R.id.et_main_pwd);
intent = new Intent();
ComponentName componentName=new ComponentName("com.example.g150831_android_qq","com.example.g150831_android_qq.QQLoginService");
intent.setComponent(componentName);
}
ServiceConnection serviceConnection=new ServiceConnection() {
private QQLoginInterfaceOut qqLoginInterfaceOut;

@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
qqLoginInterfaceOut = QQLoginInterfaceOut.Stub.asInterface(iBinder);
}

@Override
public void onServiceDisconnected(ComponentName componentName) {

}
};

@Override
protected void onResume() {
super.onResume();
bindService(intent,serviceConnection, Service.BIND_AUTO_CREATE);
}

public void login(View view){
String number=et_main_number.getText().toString();
String pwd=et_main_pwd.getText().toString();
try {
boolean flag=qqLoginInterfaceOut.login(number,pwd);
Toast.makeText(this, ""+flag, Toast.LENGTH_SHORT).show();
} catch (RemoteException e) {
e.printStackTrace();
}
}
}
以上便是本人对Android服务的总结2


                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: