博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

着手改造之前,有兴趣可以阅读下官方文档:http://help.adobe.com/zh_CN/air/extensions/index.html

新建工程 NavService 并创建包 nav.wenbo.service, 这些都可以自定义,但要记住你的包名。

注意最下方那个jar包,这个是必需的,打开项目的构建目录(buildPath),引用外部jar包, 目录在你在Flex sdk目录下

 %FlexSDK%\4.x.x\lib\android\FlashRuntimeExtensions.jar

我的本地目录是:F:\Program Files\Adobe\Adobe Flash Builder 4.7\sdks\4.6.0\lib\android\FlashRuntimeExtensions.jar

首先创建air调用的入口类 ServiceExtension 这个类必需在ane包里面的extension.xml里指定(后面会介绍)

package nav.wenbo.service;

import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.adobe.fre.FREContext;
import com.adobe.fre.FREExtension;

public class ServiceExtension implements FREExtension {
    public static final String TAG = "ServiceExtension";
    public static Context appContext;
    public static FREContext extensionContext;
    public static String Msg = "none";
    
    @Override
    public FREContext createContext(String contextType) {
        // TODO Auto-generated method stub
        return new ServiceExtensionContext();
    }

    @Override
    public void dispose() {
        // TODO Auto-generated method stub
        Log.d(TAG, "Extension disposed.");
    }

    @Override
    public void initialize() {
        // TODO Auto-generated method stub
        Log.d(TAG, "Extension initialized.");
    }

}

 

ServiceExtension中关键是 createContext 方法,这个方法返回一个可连通air及本地Java代码的上下文。并在该上下文中定义可供air调用的方法

下面是ServiceExtensionContext类

package nav.wenbo.service;

import java.util.HashMap;
import java.util.Map;

import nav.wenbo.service.functions.InitFunction;
import nav.wenbo.service.functions.SendFunction;
import nav.wenbo.service.functions.StartFunction;

import android.util.Log;

import com.adobe.fre.FREContext;
import com.adobe.fre.FREFunction;

public class ServiceExtensionContext extends FREContext {
    public static final String TAG = "ServiceExtensionContext";
    @Override
    public void dispose() {
        // TODO Auto-generated method stub
        Log.d(TAG,"Context disposed.");
    }

    @Override
    public Map<String, FREFunction> getFunctions() {
        Map<String, FREFunction> functions = new HashMap<String, FREFunction>();
        functions.put("init", new InitFunction());
        functions.put("service", new StartFunction());
        functions.put("send", new SendFunction());
        
        return functions;
    }

}

 

functions.put相当注册调用逻辑的调用名, 这里把你所有调用方法都填上。init 用于初始化上下文,service用来启动和关闭服务, send用于发送消息。 在编写对应方法之前,

先把上一节的Backgroundservice 移植过来

package nav.wenbo.service;

import com.wenbo.navservice.R;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

public class NotificationService extends Service {
    private NotificationManager notificationMgr;
    private Thread mthr;
    private int mCount=0;
    private Boolean mSend=true;
    
    @Override
    public void onCreate() {
        super.onCreate();
        notificationMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        displayNotificationMessage("starting Background Service");
        
        if(mthr == null || mSend == false) 
        {
            mSend=true;
            mthr = new Thread(null, new ServiceWorker(), "BackgroundSercie");
            mthr.start();
        }
        if(null != ServiceExtension.extensionContext) ServiceExtension.extensionContext.dispatchStatusEventAsync("start", "1");
    }
    
    @Override
    public void onDestroy()
    {
        super.onDestroy();
        mSend = false;
    }
    
    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }

    class ServiceWorker implements Runnable {
        @Override
        public void run() {
            // do background processing here.....
            // stop the service when done...
            // BackgroundService.this.stopSelf()
            while(mSend)
            {
                try{
                    Thread.sleep(1000);
                    Log.d("", "runnable" + mCount);
                    displayNotificationMessage(ServiceExtension.Msg);
                }
                catch (InterruptedException e)
                {
                    e.printStackTrace();
                }
            }
        }
    }
    
    private void displayNotificationMessage(String message) {
        if(message == "none") return;
        
        Log.d("", message);
        mCount++;
        Notification notification = new Notification(R.drawable.ic_launcher, message,
                System.currentTimeMillis());
        
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, Activity.class), 0);
        
        notification.setLatestEventInfo(this, "女神之贱", message, contentIntent);
        
        notificationMgr.notify(1000, notification);
    }
}

 

这里改动很小,发送的消息从ServiceExtension类里面取,当message != none时显示该通知,另ServiceExtension.extensionContext.dispatchStatusEventAsync 是用来给air程序发消息, 在air中监听StatusEvent.STATUS 可以捕获这条消息并获取对应参数。在这里用处是通知服务启动成功。

下面就剩下实际调用的方法了。

首先是初始化上下文,这里主要是ServiceExtension.extensionContext 主要用于往air发消息。

package nav.wenbo.service.functions;

import nav.wenbo.service.ServiceExtension;

import android.content.Context;
import android.util.Log;

import com.adobe.fre.FREContext;
import com.adobe.fre.FREFunction;
import com.adobe.fre.FREObject;

public class InitFunction implements FREFunction {
    public static final String TAG = "InitFunction";
    @Override
    public FREObject call(FREContext context, FREObject[] args) {
        ServiceExtension.extensionContext = context;
        
        Context appContext = context.getActivity().getApplicationContext();
        ServiceExtension.appContext = appContext;
        
        
        Log.i(TAG, "in init");
        
        return null;
    }

}

这里注意了,因为我们没有独立的Android入口文件,我们 只能从 FREContext.getActivity() 来取得我们所需的上下文,并用这上下文启动我们的服务,这是跟前一节例子最大不同的地方。

执行后通知air:context.dispatchStatusEventAsync("start", "2");

package nav.wenbo.service.functions;

import nav.wenbo.service.NotificationService;
import android.content.Context;
import android.content.Intent;

import com.adobe.fre.FREContext;
import com.adobe.fre.FREFunction;
import com.adobe.fre.FREObject;

public class StartFunction implements FREFunction {

    @Override
    public FREObject call(FREContext context, FREObject[] args) {
        Context appContext = context.getActivity().getApplicationContext();
        Boolean isStart=true;
        try {
            isStart = args[0].getAsBool();
        } catch (Exception e) {
            
        }
        if(isStart) context.getActivity().startService(new Intent(appContext, NotificationService.class));
        else context.getActivity().stopService(new Intent(appContext, NotificationService.class));
        
        context.dispatchStatusEventAsync("start", "2");
        return null;
    }

}

 

最后这个最简单,仅仅设置下所要推送的消息.

package nav.wenbo.service.functions;

import nav.wenbo.service.ServiceExtension;

import com.adobe.fre.FREContext;
import com.adobe.fre.FREFunction;
import com.adobe.fre.FREObject;

public class SendFunction implements FREFunction {

    @Override
    public FREObject call(FREContext context, FREObject[] args) {
        // TODO Auto-generated method stub
        String msg="none";
        try
        {
            msg = args[0].getAsString();
        }
        catch(Exception e)
        {
            
        }
        ServiceExtension.Msg = msg;
        return null;
    }

}

 

 

做完这些,并设置好权限后,把项目导出jar包, 命名为 libAndroidServiceLib.jar

下一节我将介绍air中跟此jar包对接。