守望者--AIR技术交流

标题: 通过 ANE(Adobe Native Extension) 启动Andriod服务 推送消息(一) [打印本页]

作者: 破晓    时间: 2015-1-20 14:13
标题: 通过 ANE(Adobe Native Extension) 启动Andriod服务 推送消息(一)

项目组用air来开发手游, 但有些在原生应用里很容易实现的功能没有办法在air中直接调用,比如说震动,服务等等。但Adobe 提供了一种方法让air间接调用本地代码(java,object-c...),就是接下来要介绍的ANE(Adobe Native Extension) 也叫本地扩展。

查了下资料,早在2011年11月 Adobe 官方就发一篇介绍ANE的文章附一个简单的例子, 在去年八月份Adobe 开发者中心 开始发一系列较为详尽的文章, 有兴趣可以阅读下:

http://www.adobe.com/cn/devnet/air/articles/developing-native-extensions-air.html

http://www.adobe.com/cn/devnet/air/air_for_android.html


我先照着官方例子,做了一个调节多好媒体音量的扩展,并在测试机器正常运转。于是我开始着手准备项目需求 -- 利用Andriod 服务来推送应用消息, 于是也有了这系列文章的由来,接下来我将介绍我做的一些工作。

一、 HellAndriod Service

  由于我这前没有做过Andriod 开发,对java也不是很熟悉,唯一的Java编程经历是在大学时参与过的J2EE的项目,所以我先做了一个Andriod 服务的本地应用练练手. 这样的例子在网上很多,略作修改,代码如下:

  1. package com.wenbo.helloandriod;

  2. import android.app.Notification;

  3. public class BackgroundService extends Service {

  4.     private NotificationManager notificationMgr;
  5.     private Thread mthr;
  6.     private int mCount=0;
  7.     private Boolean mSend=true;
  8.     @Override
  9.     public void onCreate() {
  10.         super.onCreate();
  11.         notificationMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
  12.         displayNotificationMessage("starting Background Service");
  13.         
  14.         if(mthr == null || mSend == false)
  15.         {
  16.             mSend=true;
  17.             mthr = new Thread(null, new ServiceWorker(), "Background Service");
  18.             mthr.start();
  19.         }
  20.         
  21.     }
  22.    
  23.     @Override
  24.     public void onDestroy()
  25.     {
  26.         super.onDestroy();
  27.         mSend = false;
  28.     }

  29.     @Override
  30.     public IBinder onBind(Intent arg0) {
  31.         // TODO Auto-generated method stub
  32.         return null;
  33.     }
  34.    
  35.     class ServiceWorker implements Runnable {
  36.         @Override
  37.         public void run() {
  38.             // do background processing here.....
  39.             // stop the service when done...
  40.             // BackgroundService.this.stopSelf()
  41.             while(mSend)
  42.             {
  43.                 try{
  44.                     Thread.sleep(1000);
  45.                     Log.d("", "runnable" + mCount);
  46.                     displayNotificationMessage("runnable" + mCount);
  47.                 }
  48.                 catch (InterruptedException e)
  49.                 {
  50.                     e.printStackTrace();
  51.                 }
  52.             }
  53.         }
  54.     }

  55.     private void displayNotificationMessage(String message) {
  56.         Log.d("", message);
  57.         mCount++;
  58.         @SuppressWarnings("deprecation")
  59.         Notification notification = new Notification(R.drawable.ic_launcher, message,
  60.                 System.currentTimeMillis());
  61.         
  62.         PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), 0);
  63.         
  64.         notification.setLatestEventInfo(this, "女神之贱", message, contentIntent);
  65.         
  66.         notificationMgr.notify(R.id.app_notification_id, notification);
  67.     }
  68. }
复制代码
在服务里开个线程,每隔一秒发一个后台通知。 然后我们建立一个入口启动它。

  1. package com.wenbo.helloandriod;

  2. import android.os.Bundle;

  3. public class MainActivity extends Activity {
  4.     private static final String TAG = "MainActivity";
  5.     @Override
  6.     protected void onCreate(Bundle savedInstanceState) {
  7.         super.onCreate(savedInstanceState);
  8.         setContentView(R.layout.activity_main);
  9.         
  10.         Log.d(TAG, "starting service");
  11.         
  12.         Button bindBtn = (Button) findViewById(R.id.start);
  13.         bindBtn.setOnClickListener(new OnClickListener() {
  14.             @Override
  15.             public void onClick(View v) {
  16.                 // TODO Auto-generated method stub
  17.                 startService(new Intent(MainActivity.this,
  18.                         BackgroundService.class));
  19.             }
  20.         });
  21.         
  22.         Button unbindBtn = (Button) findViewById(R.id.stop);
  23.         unbindBtn.setOnClickListener(new OnClickListener() {
  24.             
  25.             @Override
  26.             public void onClick(View v) {
  27.                 // TODO Auto-generated method stub
  28.                 stopService(new Intent(MainActivity.this, BackgroundService.class));
  29.             }
  30.         });
  31.     }

  32.     @Override
  33.     public boolean onCreateOptionsMenu(Menu menu) {
  34.         // Inflate the menu; this adds items to the action bar if it is present.
  35.         getMenuInflater().inflate(R.menu.main, menu);
  36.         return true;
  37.     }

  38. }
复制代码
做完这些,别忘了配置权限, 在AndroidManifest.xml的<activity></activity>后面添加

  1. <service android:name="BackgroundService"/>
复制代码
安装到模拟器上或真实机器上 打开程序启动后就可以看到每隔一秒后台消息便会更新一次。按下stop后停止更新。好了,下一节我将要按照ANE的要求改造它。由于不能上传附件,我将另两个关键文件也贴出来。一个是res/layout/activity_main.xml
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2.     xmlns:tools="http://schemas.android.com/tools"
  3.     android:layout_width="match_parent"
  4.     android:layout_height="match_parent"
  5.     android:paddingBottom="@dimen/activity_vertical_margin"
  6.     android:paddingLeft="@dimen/activity_horizontal_margin"
  7.     android:paddingRight="@dimen/activity_horizontal_margin"
  8.     android:paddingTop="@dimen/activity_vertical_margin"
  9.     tools:context=".MainActivity" >

  10.     <TextView
  11.         android:id="@+id/textView1"
  12.         android:layout_width="wrap_content"
  13.         android:layout_height="wrap_content"
  14.         android:text="@string/hello_world" />

  15.     <Button
  16.         android:id="@+id/start"
  17.         android:layout_width="wrap_content"
  18.         android:layout_height="wrap_content"
  19.         android:layout_alignLeft="@+id/textView1"
  20.         android:layout_below="@+id/textView1"
  21.         android:text="start" />

  22.     <Button
  23.         android:id="@+id/stop"
  24.         android:layout_width="wrap_content"
  25.         android:layout_height="wrap_content"
  26.         android:layout_alignLeft="@+id/start"
  27.         android:layout_below="@+id/start"
  28.         android:layout_marginTop="15dp"
  29.         android:text="stop" />

  30. </RelativeLayout>
复制代码
另一个是AndroidManifest.xml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3.     package="com.wenbo.helloandriod"
  4.     android:versionCode="1"
  5.     android:versionName="1.0" >

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

  9.     <application
  10.         android:allowBackup="true"
  11.         android:icon="@drawable/ic_launcher"
  12.         android:label="@string/app_name"
  13.         android:theme="@style/AppTheme" >
  14.         <activity
  15.             android:name="com.wenbo.helloandriod.MainActivity"
  16.             android:label="@string/app_name" >
  17.             <intent-filter>
  18.                 <action android:name="android.intent.action.MAIN" />

  19.                 <category android:name="android.intent.category.LAUNCHER" />
  20.             </intent-filter>
  21.         </activity>
  22.         <service android:name="BackgroundService"/>
  23.     </application>

  24. </manifest>
复制代码
本文来自:http://www.cnblogs.com/bobolive/p/3201062.html



作者: 破晓    时间: 2015-1-20 14:40
本系列文章目录








欢迎光临 守望者--AIR技术交流 (http://www.airmyth.com/)