首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用phonegap在android应用程序中24小时跟踪用户

如何使用phonegap在android应用程序中24小时跟踪用户
EN

Stack Overflow用户
提问于 2013-08-05 14:16:08
回答 1查看 225关注 0票数 1

我有一个跟踪用户的特定要求,一旦他提交了未来24小时的数据,我使用phonegap和html5制作了一款安卓应用程序。我在链接中查看了android的后台服务

代码语言:javascript
复制
https://github.com/phonegap/phonegap-plugins/tree/master/Android/BackgroundService

但是,如果不了解如何开始使用it.any帮助,我们将非常感谢。

EN

回答 1

Stack Overflow用户

发布于 2013-08-05 18:27:49

教程:

http://red-folder.blogspot.com/2012/09/phonegap-android-background-service.html

我认为你必须用android写一个服务(Java),它很简单,很简单。

代码语言:javascript
复制
@Override
public void onCreate() {  
    Calendar cal = Calendar.getInstance();
    Intent intent = new Intent(this, MyService.class);
    PendingIntent pintent = PendingIntent.getService(this, 0, intent, 0);
    AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
    alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 60*1000, pintent);
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) { 
    if(isOnline())
        text="Network available.";
    else
        text="Network not available.";
    showNotification(text);
    return START_STICKY;
}

@Override
public void onDestroy() {
}

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

@SuppressWarnings("deprecation")
void showNotification(String text){
    mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
    notification = new Notification(R.drawable.ic_launcher, text,System.currentTimeMillis());
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    //notification.flags |= Notification.DEFAULT_SOUND;
    //resultIntent = getPackageManager().getLaunchIntentForPackage("com.example.hello");
    resultIntent = new Intent( this , MainActivity.class );
    contentIntent = PendingIntent.getActivity( this, 0,resultIntent, 0 );
    notification.setLatestEventInfo( this, "Internet", text , contentIntent );
    mNM.notify(1, notification);
}

public boolean isOnline() {
    connMgr = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
    networkInfo = connMgr.getActiveNetworkInfo();
    return (networkInfo != null && networkInfo.isConnected());
}

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18051657

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档