通过 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 服务的本地应用练练手. 这样的例子在网上很多,略作修改,代码如下:
package com.wenbo.helloandriod;
import android.app.Notification;
public class BackgroundService 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(), "Background Service");
mthr.start();
}
}
@Override
public void onDestroy()
{
super.onDestroy();
mSend = false;
}
@Override
public IBinder onBind(Intent arg0) {
// 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("runnable" + mCount);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
}
private void displayNotificationMessage(String message) {
Log.d("", message);
mCount++;
@SuppressWarnings("deprecation")
Notification notification = new Notification(R.drawable.ic_launcher, message,
System.currentTimeMillis());
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), 0);
notification.setLatestEventInfo(this, "女神之贱", message, contentIntent);
notificationMgr.notify(R.id.app_notification_id, notification);
}
}
在服务里开个线程,每隔一秒发一个后台通知。 然后我们建立一个入口启动它。
package com.wenbo.helloandriod;
import android.os.Bundle;
public class MainActivity extends Activity {
private static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(TAG, "starting service");
Button bindBtn = (Button) findViewById(R.id.start);
bindBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
startService(new Intent(MainActivity.this,
BackgroundService.class));
}
});
Button unbindBtn = (Button) findViewById(R.id.stop);
unbindBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
stopService(new Intent(MainActivity.this, BackgroundService.class));
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
做完这些,别忘了配置权限, 在AndroidManifest.xml的<activity></activity>后面添加
<service android:name="BackgroundService"/>
安装到模拟器上或真实机器上 打开程序启动后就可以看到每隔一秒后台消息便会更新一次。按下stop后停止更新。
好了,下一节我将要按照ANE的要求改造它。
由于不能上传附件,我将另两个关键文件也贴出来。
一个是res/layout/activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
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"
tools:context=".MainActivity" > <TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" /> <Button
android:id="@+id/start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:text="start" /> <Button
android:id="@+id/stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/start"
android:layout_below="@+id/start"
android:layout_marginTop="15dp"
android:text="stop" /> </RelativeLayout>
另一个是AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.wenbo.helloandriod"
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.wenbo.helloandriod.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name="BackgroundService"/>
</application> </manifest>
通过 ANE(Adobe Native Extension) 启动Andriod服务 推送消息(一)的更多相关文章
- 通过 ANE(Adobe Native Extension) 启动Andriod服务 推送消息(三)
jar包完成后,剩下就是要构建ANE包来供实际程序调用. 首先要建两个Flex库项目, default那个是官方建议加上的,仅用于不在真实环境下编译调试的时候有个默认接口不至于调用不成功报错,项目结构 ...
- 通过 ANE(Adobe Native Extension) 启动Andriod服务 推送消息(二)
着手改造之前,有兴趣可以阅读下官方文档:http://help.adobe.com/zh_CN/air/extensions/index.html 新建工程 NavService 并创建包 nav.w ...
- 通过 ANE(Adobe Native Extension) 启动Andriod服务 推送消息(四)
这一节,是要把AS库和Android的jar包及相关配置文件打成一个ane包. 首先先建一个build目录,里面文件目录结构如下: 然后用打开压缩包的方式打开ServiceLib.swc, 把其中的l ...
- 通过 ANE(Adobe Native Extension) 启动Andriod服务 推送消息(五)
这一节,用个简单的例子来调用下之前生成的service.ane 首先建一个flex手机项目 然后在构建路径中把ane引进来 可以看到此ane支持Android平台. serviceMobile.mxm ...
- 用JPUSH极光推送实现服务端向安装了APP应用的手机推送消息(C#服务端接口)
这次公司要我们做一个功能,就是当用户成功注册以后,他登录以后要收到消息,当然这个消息是安装了我们的手机APP应用的手机咯. 极光推送的网站的网址是:https://www.jpush.cn/ 极光推送 ...
- APNS 服务推送通知
1. 将app注册notification里面, 并从APNS上获取测试机的deviceToken. - (BOOL)application:(UIApplication *)application ...
- 模拟websocket推送消息服务mock工具二
模拟websocket推送消息服务mock工具二 在上一篇博文中有提到<使用electron开发一个h5的客户端应用创建http服务模拟后端接口mock>使用electron创建一个模拟后 ...
- Spring Boot 集成 WebSocket 实现服务端推送消息到客户端
假设有这样一个场景:服务端的资源经常在更新,客户端需要尽量及时地了解到这些更新发生后展示给用户,如果是 HTTP 1.1,通常会开启 ajax 请求询问服务端是否有更新,通过定时器反复轮询服务端响应的 ...
- 使用极光推送(www.jpush.cn)向安卓手机推送消息【服务端向客户端主送推送】C#语言
在VisualStudio2010中新建网站JPushAndroid.添加引用json帮助类库Newtonsoft.Json.dll. 在web.config增加appkey和mastersecret ...
随机推荐
- jenkins持续集成配置备忘
配过好几次,但是老忘记.记录备忘. 安装下列插件: ssh和git插件用来下载源码 publish over ssh 插件用来发布代码到测试环境. email插件用来更强大的email配置. file ...
- new Date() iso不支持兼容性问题
在IOS5以上版本(不包含IOS5)中的Safari浏览器能正确解释出Javascript中的 new Date('2013-10-21') 的日期对象. 但是在IOS5版本里面的Safari解释ne ...
- POJ 1179 IOI1998 Polygon
Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 5472 Accepted: 2334 Description Polyg ...
- Java字节转换类实现
Java的类库支持完全不如C#,比如时间类,比如数据类型转换类等等,难道是我自己没找到吗? 下面是字节转换类,byte[]与short, int, long, float, double, Strin ...
- JavaScript高级程序设计—阅读笔记(第一部分)
第一章 JavaScript简介1.JavaScript组成(核心(ECMAScript),文档对象模型(DOM),浏览器对象模型(BOM))2.文档对象模型(DOM),提供访问和操作网页内容的方法和 ...
- Android内存分析工具DDMS heap + MAT 安装和使用
一 Java内存分析工具扫盲 如果像我一样一点都不了解,可以先进行内存分析工具扫盲 MAT介绍: Eclipse Memory Analyzer(MAT)一个功能丰富的 JAVA 堆转储 ...
- 软中断&tasklet&工作队列
软中断 软中断的分配时静态的(即在编译时定义),而tasklet的分配和初始化能够在执行时进行. 软中断(即便是同一种类型的软中断)能够并发地运行在多个CPU上. 因此,软中断是可重入函数并且必须明白 ...
- Yii CGridView 基本使用(三)关联表相关字段搜索
加入 关联表 相关字段的搜索: 先说一句,我们在这里仅仅谈 "一对多" 的关联搜索,首先,不要忘了我们的数据库,忘记的同学请戳这里:这里.能够看到在 tbl_post 中是有一个外 ...
- Java SE学习之printf 日期转换符
本文是学习网络上的文章时的总结,感谢大家无私的分享. System.out.printf()方法能够对日期做处理输出. 相应列表 转换符 类型 举例 c 完整的日期和时间 Mon Feb 09 18: ...
- POJ 3670 , 3671 LIS
题意:两题意思差不多,都是给你一个序列,然后求最少需要改变多少个数字,使得成为一个最长不升,或者最长不降子序列. 当然3671是只能升序,所以更简单一点. 然后就没有什么了,用二分的方法求LIS即可. ...