我有一个跟踪用户的特定要求,一旦他提交了未来24小时的数据,我使用phonegap和html5制作了一款安卓应用程序。我在链接中查看了android的后台服务
https://github.com/phonegap/phonegap-plugins/tree/master/Android/BackgroundService但是,如果不了解如何开始使用it.any帮助,我们将非常感谢。
发布于 2013-08-05 18:27:49
教程:
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());
}
https://stackoverflow.com/questions/18051657
复制相似问题