151102笔记

Android studio AIDL
首先右键建立AIDL

package com.example.administrator.talkapp;

// Declare any non-default types here with import statements

interface MyAIDLService {
    int plus(int a, int b);
        String toUpperCase(String str);
}

注意那个包名
来到清单,要保持包名一致
然后build-makeproject,就可以看到
这里写图片描述
然后就可以开始启用了

package com.example.administrator.talkapp;

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;
import android.widget.Button;

/**
 * Created by Administrator on 2015/11/2.
 */
public class MainActivity extends Activity implements View.OnClickListener{
    private Button startService;

    private Button stopService;

    private Button bindService;

    private Button unbindService;
    private MyService.MyBinder myBinder;


    private MyAIDLService myServiceAIDL;
    private Intent binderIntent;
    private final static boolean create_flag=true;
    private final static boolean destory_flag=false;
    private final static String TAG="MainActivity";






    private ServiceConnection connection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
//            myBinder=(MyService.MyBinder)iBinder;
//            myBinder.startDownload();
            myServiceAIDL=MyAIDLService.Stub.asInterface(iBinder);
            try{
                //用过AIDL远程调用
                Log.e("fish","aidl");
                myServiceAIDL.toUpperCase("lala");
            }catch (Exception e)
            {
                e.printStackTrace();
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {

        }
    };




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
//        startService = (Button) findViewById(R.id.start_service);
//        stopService = (Button) findViewById(R.id.stop_service);
//        bindService = (Button) findViewById(R.id.bind_service);
//        unbindService = (Button) findViewById(R.id.unbind_service);
//        startService.setOnClickListener(this);
//        stopService.setOnClickListener(this);
//        bindService.setOnClickListener(this);
//        unbindService.setOnClickListener(this);
        //开启服务
        Intent intent = new Intent(this, MyService.class);
        startService(intent);

        //连接远程Service和Activity
        binderIntent = new Intent(this,MyService.class);
        Bundle bundle = new Bundle();
        bundle.putBoolean("flag", create_flag);
        binderIntent.putExtras(bundle);
        bindService(binderIntent, connection, BIND_AUTO_CREATE);


    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.start_service:
                Intent startIntent = new Intent(this, MyService.class);
                startService(startIntent);
                break;
            case R.id.stop_service:
                Intent stopIntent = new Intent(this, MyService.class);
                stopService(stopIntent);
                break;
            case R.id.bind_service:
                Intent bindIntent = new Intent(this, MyService.class);
                bindService(bindIntent, connection, BIND_AUTO_CREATE);
                break;
            case R.id.unbind_service:
                unbindService(connection);
                break;
            default:
                break;
        }
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d(TAG,"++MainActivity onDestroy++");

        boolean flag = false;
        //暂停服务
        Intent intent = new Intent(this, MyService.class);
        stopService(intent);

        //断开与远程Service的连接
        unbindService(connection);
    }





}
package com.example.administrator.talkapp;

import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.annotation.Nullable;
import android.util.Log;

/**
 * Created by Administrator on 2015/11/2.
 */
public class MyService extends Service {

//    private MyBinder mBinder = new MyBinder();
    boolean flag;
    private final static String TAG = "MainService";
MyAIDLService.Stub mbinder=new MyAIDLService.Stub() {
    @Override
    public int plus(int a, int b) throws RemoteException {
        return 0;
    }

    @Override
    public String toUpperCase(String str) throws RemoteException {

        return null;
    }
};
    @Override
    public void onCreate() {
        super.onCreate();
//        Log.d(TAG, "onCreate() executed");
//        Notification notification = new Notification(R.drawable.ic_check,
//                "有通知到来", System.currentTimeMillis());
//        Intent notificationIntent = new Intent(this, MainActivity.class);
//        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
//                notificationIntent, 0);
        notification.setLatestEventInfo(this, "这是通知的标题", "这是通知的内容",
                pendingIntent);
        startForeground(1, notification);
//        Notification n = new Notification.Builder(this)
//                .setContentTitle("有通知到来")
//                .setTicker("Ticker")
//                .setContentText("内容")
//                .setSmallIcon(R.drawable.ic_check)
//                .setContentIntent(pendingIntent)
//                .build();
//
//        startForeground(1, n);
        try{
            Thread.sleep(1000);
        }catch (Exception e)
        {
            e.printStackTrace();
        }
        Log.d(TAG, "onCreate() executed");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d(TAG, "onStartCommand() executed");
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "onDestroy() executed");
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }


    class MyBinder extends Binder {

        public void startDownload() {
            Log.d("TAG", "startDownload() executed");
            // 执行具体的下载任务
        }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值