前台service和远程service代码详解

因为之前对service不是很熟,所以进一步研究了一下

//有时候需要前台service,如墨迹天气(在状态栏中).下面是简单的一个前台Service

public class MyService extends Service {

	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}
	
	@Override
	public void onCreate() {
		super.onCreate();
		System.out.println("------------->执行");
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				
				Notification notification = new Notification(R.drawable.ic_launcher, "有通知到来", System.currentTimeMillis());  
		        Intent notificationIntent = new Intent(MyService.this, Main.class);  
		        PendingIntent pendingIntent = PendingIntent.getActivity(MyService.this, 0, notificationIntent, 0); //设置信息内容   
		        notification.setLatestEventInfo(MyService.this, "这是通知的标题", "这是通知的内容", pendingIntent);  
		        startForeground(1, notification);  //让Service变成一个前台Service
		        
			}
		}).start();
	}

}

/*************************************************************AIDL Service**********************************************************************************************************************/

服务端:新建一个工程:AIDL_Service

新建一个aidl文件

package com.example.aidl;
interface AIDLservice{  
    int plus(int a, int b);
}  

//MyService.java

public class AIDLservice extends Service {

	@Override
	public IBinder onBind(Intent intent) {
		return stub;
	}
	
	//在AIDLservice.java文件可知道Stub是Binder的子类
	com.example.aidl.AIDLservice.Stub stub=new Stub() {
		
		@Override
		public int plus(int a, int b) throws RemoteException {
			
			return a+b;
		}
	};
	

}
//androidmanifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.aidl_service"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.aidl_service.Main"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <!--添加此属性:android:process=":remote" 表示为远程service-->
        <service android:name="com.example.aidl_service.AIDLservice"
                 android:process=":remote">
            <intent-filter >
                 <!--此处体现了隐示Intent-->
                <action android:name="com.example.aidl_service.startAIDLservice"/>
            </intent-filter>
        </service>
    </application>

</manifest>

//客户端:新建工程AIDL_Client

/**
 * 要实现远程Service的功能,必须将远程Service端的aidl文件原始的复制到客户端的src下*/
public class Main extends Activity {
     
	private Button button;
	private AIDLservice aidLservice;//远程Service接口
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		//匿名类,主要是通过该匿名类与service通信
		final ServiceConnection conn=new ServiceConnection() {
			
			@Override
			public void onServiceDisconnected(ComponentName name) {
				
			}
			
			@Override
			public void onServiceConnected(ComponentName name, IBinder service) {
				try {
					
					aidLservice=AIDLservice.Stub.asInterface(service); 
					int num=aidLservice.plus(5, 4);
					Log.d("------------>num=", num+"");
					
				} catch (Exception e) {
					System.out.println("连接失败!!!");
					e.printStackTrace();
				}
			}
		};
		
		button=(Button)this.findViewById(R.id.button);
		button.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent=new Intent();
				intent.setAction("com.example.aidl_service.startAIDLservice");
				bindService(intent, conn, BIND_AUTO_CREATE);
			}
		});
		
		
	}
}
猛击下载



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值