Я создал приложение, где можно запустить фоновый сервис. Эта служебная цель - отслеживать, какое приложение используется на телефоне, и сохранять эти данные вместе с меткой времени и координатами SQLite на телефоне.служба поддержки Android получить местоположение
Часть, которая находит приложение и сохраняет его с отметкой времени, отлично работает. Но часть с местом работы работает. Раньше я работал с местными исполнителями, но после многих часов и многих попыток я сдавался. Я не уверен, где разместить мой сайт? прямо сейчас я создал новый внутренний класс, но когда я его запускаю, я получаю сообщение об ошибке, указывающее, что мне нужно запустить Looper.prepare(), но это поможет. Затем он говорит, что для каждого потока может быть создан только один петлитель.
Теперь я чувствую, что независимо от того, что я пробовал что-то еще, это неправильно, и поэтому я надеюсь, что некоторые из вас, ребята, могут мне помочь.
package com.dtu.applogger;
import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.app.ActivityManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.IBinder;
import android.os.Looper;
import android.util.Log;
import android.widget.Toast;
public class loggerService extends Service{
DBAdapter dbadapter;
public MyLocationListener mMyLocationListener;
private NotificationManager mNM;
private int NOTIFICATION = 10002; //Any unique number for this notification
protected String latitude;
protected String longitude;
int counter= 0;
static final int UPDATE_INTERVAL= 5000;
private Timer timer= new Timer();
public loggerService() {
// TODO Auto-generated constructor stub
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
private void showNotification() {
// In this sample, we'll use the same text for the ticker and the expanded notification
CharSequence text = "TITLE";
// Set the icon, scrolling text and timestamp
Notification notification = new Notification(R.drawable.call_log, text, System.currentTimeMillis());
// The PendingIntent to launch our activity if the user selects this notification
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, MainActivity.class), 0);
// Set the info for the views that show in the notification panel.
notification.setLatestEventInfo(this, "Service is running", text, contentIntent);
// Send the notification.
mNM.notify(NOTIFICATION, notification);
}
public int onStartCommand(Intent intent, int flags, int startId){
//showNotification();
new DoBackgroundTask().execute();
Toast.makeText(this, "Service started", Toast.LENGTH_LONG).show();
return START_STICKY;
}
private class DoBackgroundTask extends AsyncTask{
String oldPackageName = "com.dtu.applogger";
DBAdapter dbadapter = new DBAdapter(loggerService.this);
public DoBackgroundTask() {
}
protected String findApp(){
final LocationManager lm = (LocationManager) loggerService.this.getSystemService(Context.LOCATION_SERVICE);
ActivityManager am = (ActivityManager) loggerService.this.getSystemService(Activity.ACTIVITY_SERVICE);
String packageName = am.getRunningTasks(1).get(0).topActivity.getPackageName();
if(!packageName.equals(oldPackageName) && !packageName.equals("com.dtu.applogger")){
//save oldPackageName and packageName in DB
mMyLocationListener = new MyLocationListener();
lm.requestLocationUpdates(LocationManager.PASSIVE_PROVIDER, 0, 0, mMyLocationListener);
String lat = latitude;
String lng = longitude;
String mydate = java.text.DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime());
String txt = packageName + " : " + mydate;
dbadapter.open();
if(packageName.equals("com.android.launcher")){
dbadapter.saveLog(mydate, oldPackageName, lat, lng);
}else{
dbadapter.saveLog(mydate, packageName, lat, lng);
}
dbadapter.close();
oldPackageName = packageName;
return txt;
}
if(packageName.equals(oldPackageName)){
return null;
}
return null;
}
@Override
protected Object doInBackground(Object... params) {
timer.scheduleAtFixedRate(new TimerTask() {
public void run() {
String txt = findApp();
if(txt != null){
Log.d("APP OPEN", "===== " + txt.toString());
}
Log.d("loggerService", String.valueOf(++counter));
}
}, 0, UPDATE_INTERVAL);
// TODO Auto-generated method stub
return null;
}
}
private class MyLocationListener implements android.location.LocationListener{
@Override
public void onLocationChanged(Location location) {
int lat = (int) (location.getLatitude()*1E6);
int lng = (int) (location.getLongitude()*1E6);
latitude = Integer.toString(lat);
longitude = Integer.toString(lng);
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}
public void onDestroy(){
super.onDestroy();
timer.cancel();
Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show();
}
}
Thx для этого, но не решить мою проблему с запуском другого внутреннего класса в этом случае «MyLocationListener» – freddy
Вместо AsyncTask, просто запустить все это в потоке , –