1

我有一个特定的要求,即在用户提交下一个 24 小时的数据后跟踪他我正在使用 phonegap 和 html5 来为此目的制作一个 android 应用程序。我在链接中查看了带有 android 的后台服务

https://github.com/phonegap/phonegap-plugins/tree/master/Android/BackgroundService

但不知道如何开始。任何帮助将不胜感激。

4

1 回答 1

0

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

我认为你必须在 android 中编写一个服务(java),它非常简单。

@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());
}

于 2013-08-05T10:27:48.503 回答