我的 Android 服务并非一直 运行。当我重新打开应用程序时它熄灭了。实施我的服务的正确方法是什么?
My Android service is not running all the time. it goes off when i reopen the app. what is the correct way of implementing my service?
我开发了一个 android 应用程序,其中我将广播接收器与 android 服务集成在一起。我的座右铭是只要设备连接到互联网,它就会执行一些操作。当我安装该应用程序时,它第一次运行良好。但是当我重新打开我的应用程序时,它无法正常工作。我应该在哪里声明我的服务,以便它 运行 无论应用程序打开或关闭或重新打开!无论发生什么,它都应该 运行 在 bg 中始终并在连接互联网时执行某些操作(正如我编程的那样)。我怎样才能做到这一点?下面是我的 android 服务代码
package processteam.ju;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Environment;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import java.util.logging.Handler;
import java.util.logging.LogRecord;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class AndroidService extends Service {
Timer t;
Timer t1;
TimerTask task;
TimerTask task1;
int time = 20;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
startService(new Intent(this, AndroidService.class));
return START_STICKY;
}
@Override
public void onDestroy() {
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
super.onCreate();
this.registerReceiver(this.mConnReceiver,new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
}
public void zipper(){
{
File uploadedimagedirectory = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/JU_Mobile_Files/Uploaded_Images/");
File[] imagecontents = uploadedimagedirectory.listFiles();
if (imagecontents == null) {
Toast.makeText(getApplicationContext(), "uploadedimagedirectory null", Toast.LENGTH_LONG).show();
} else if (imagecontents.length == 0) {
Toast.makeText(getApplicationContext(), "uploadedimagedirectory empty", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), "uploadedimagedirectory 1st line", Toast.LENGTH_LONG).show();
try {
String inputFolderPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/JU_Mobile_Files/Uploaded_Images/";
String outZipPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/JU_Mobile_Files/Images_Media_Zip/images.zip";
Toast.makeText(this, "uploadimg2dp try block" + inputFolderPath + " ---" + outZipPath, Toast.LENGTH_LONG).show();
FileOutputStream fos = new FileOutputStream(outZipPath);
ZipOutputStream zos = new ZipOutputStream(fos);
Toast.makeText(this, "What i get in uploadimg2dp" + inputFolderPath + " <--> " + outZipPath, Toast.LENGTH_LONG).show();
File srcFile = new File(inputFolderPath);
File[] files = srcFile.listFiles();
Toast.makeText(this, "S.no " + srcFile.getName(), Toast.LENGTH_LONG).show();
for (int i = 0; i < files.length; i++) {
byte[] buffer = new byte[1024];
FileInputStream fis = new FileInputStream(files[i]);
zos.putNextEntry(new ZipEntry(files[i].getName()));
int length;
while ((length = fis.read(buffer)) > 0) {
zos.write(buffer, 0, length);
}
zos.closeEntry();
fis.close();
}
zos.close();
} catch (IOException ioe) {
Log.e("", ioe.getMessage());
}
}
File uploadedvideodirectory = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/JU_Mobile_Files/Uploaded_Videos/");
File[] videocontents = uploadedvideodirectory.listFiles();
if (videocontents == null) {
Toast.makeText(getApplicationContext(), "uploadedvideodirectory null", Toast.LENGTH_LONG).show();
} else if (videocontents.length == 0) {
Toast.makeText(getApplicationContext(), "uploadedvideodirectory empty", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), "uploadedvideodirectory executed", Toast.LENGTH_LONG).show();
try {
String inputFolderPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/JU_Mobile_Files/Uploaded_Videos";
String outZipPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/JU_Mobile_Files/Videos_Media_Zip/videos.zip";
Toast.makeText(this, "exv" + inputFolderPath + " ---" + outZipPath, Toast.LENGTH_LONG).show();
FileOutputStream fos = new FileOutputStream(outZipPath);
ZipOutputStream zos = new ZipOutputStream(fos);
Toast.makeText(this, "What i get in zip2 " + inputFolderPath + " <--> " + outZipPath, Toast.LENGTH_LONG).show();
File srcFile = new File(inputFolderPath);
File[] files = srcFile.listFiles();
Log.d("", "Zip directory: " + srcFile.getName());
Toast.makeText(this, "S.no " + srcFile.getName(), Toast.LENGTH_LONG).show();
for (int i = 0; i < files.length; i++) {
Log.d("", "Adding file: " + files[i].getName());
byte[] buffer = new byte[1024];
FileInputStream fis = new FileInputStream(files[i]);
zos.putNextEntry(new ZipEntry(files[i].getName()));
int length;
Log.d("ranjithkpr","8148580586ly");
while ((length = fis.read(buffer)) > 0) {
zos.write(buffer, 0, length);
}
zos.closeEntry();
fis.close();
Log.d("ranjithkpr","8148580586hy");
}
zos.close();
Log.d("ranjithkpr","8148580586yt");
} catch (IOException ioe) {
Log.e("", ioe.getMessage());
}
}
Log.d("ranjithkpr","8148580586w");
uploadmedia2dp();
Log.d("ranjithkpr","8148580586y");
}
}
public void startTimer() {
Log.d("Download123456", "Starts123456");
t = new Timer();
task = new TimerTask() {
@Override
public void run() {
DBUploaderDownloader mydb = new DBUploaderDownloader(getApplicationContext(), 0);
//mydb.execute("1");
}
};
SimpleDateFormat currenthour = new SimpleDateFormat("HH");
SimpleDateFormat currentime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
SimpleDateFormat currentdate = new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = Calendar.getInstance();
cal.add(Calendar.HOUR, 1);
int hours = Integer.parseInt(currenthour.format(cal.getTime()));
String target = currentdate.format(new Date());
for (int i = 0; i < 24; i = i + 1) {
if (hours == i) {
String ss = String.valueOf(i);
if (i < 10) {
ss = "0" + ss;
}
target = target + " " + ss + ":30:00";
break;
}
}
String currentdttime = currentime.format(new Date());
Date targetedtime = null;
try {
targetedtime = currentime.parse(target);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
long diff = targetedtime.getTime() - (new Date()).getTime();
//t.scheduleAtFixedRate(task, 100, 20000);
//t.scheduleAtFixedRate(task, diff, 3600000);
t.scheduleAtFixedRate(task, 100, 1800000);
}
public void downloaddb() {
}
public void startUploadTimer() {
Log.d("Upload123456", "Starts123456");
t1 = new Timer();
task1 = new TimerTask() {
@Override
public void run() {
/* DBUploaderDownloader mydb = new DBUploaderDownloader(getApplicationContext(), 0);
mydb.execute("2");*/
downloaddb();
}
};
SimpleDateFormat currenthour = new SimpleDateFormat("HH");
SimpleDateFormat currentime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
SimpleDateFormat currentdate = new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = Calendar.getInstance();
cal.add(Calendar.HOUR, 1);
int hours = Integer.parseInt(currenthour.format(cal.getTime()));
String target = currentdate.format(new Date());
for (int i = 0; i < 24; i = i + 1) {
if (hours == i) {
String ss = String.valueOf(i);
if (i < 10) {
ss = "0" + ss;
}
target = target + " " + ss + ":00:00";
break;
}
}
String currentdttime = currentime.format(new Date());
Date targetedtime = null;
try {
targetedtime = currentime.parse(target);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
long diff = targetedtime.getTime() - (new Date()).getTime();
Log.d("1234","diff="+String.valueOf(diff));
//t1.scheduleAtFixedRate(task1, 100, 360000);
//t1.scheduleAtFixedRate(task1, diff, 3600000);
//t1.scheduleAtFixedRate(task1, 100, 1800000);
t1.scheduleAtFixedRate(task1, 100, 1200000);
}
public void uploadmedia2dp(){
Log.d("ranjithkpr","8148580586");
/*Intent dialogIntent = new Intent(getBaseContext(), DropBoxActivity.class);
dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplication().startActivity(dialogIntent);*/
Intent in=new Intent().setClass(AndroidService.this,DropBoxActivity.class);
in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(in);
// Toast.makeText(getApplicationContext(),"uploadmedia2dp executed",Toast.LENGTH_LONG).show();
Log.d("ranjithkpr","814858058678");
}
private BroadcastReceiver mConnReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
try {
boolean noConnectivity = intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
String reason = intent.getStringExtra(ConnectivityManager.EXTRA_REASON);
boolean isFailover = intent.getBooleanExtra(ConnectivityManager.EXTRA_IS_FAILOVER, false);
NetworkInfo currentNetworkInfo = (NetworkInfo) intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
NetworkInfo otherNetworkInfo = (NetworkInfo) intent.getParcelableExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO);
if (currentNetworkInfo.isConnected()) {
if (t != null) {
t.cancel();
}
if (t1 != null) {
t1.cancel();
}
Toast.makeText(getApplicationContext(),"Connected",Toast.LENGTH_LONG).show();
//startTimer();
//startUploadTimer();
//uploadvideo2dp();
//uploadmedia2dp();
//uploadimage2dp();
zipper();
RecursiveFileObserver recursiveFileObserver = new RecursiveFileObserver(Environment.getExternalStorageDirectory().getAbsolutePath() + "/JU_Mobile_Files/Uploaded_Videos/");
recursiveFileObserver.startWatching();
Log.d("ranjithkpr","8148580586ggy");
RecursiveFileObserver recursiveFileObserver2 = new RecursiveFileObserver(Environment.getExternalStorageDirectory().getAbsolutePath() + "/JU_Mobile_Files/Uploaded_Images/");
recursiveFileObserver2.startWatching();
Log.d("ranjithkpr","8148580586yre");
} else {
if (t != null) {
t.cancel();
}
if (t1 != null) {
t1.cancel();
}
Toast.makeText(getApplicationContext(),"DisConnected",Toast.LENGTH_LONG).show();
}
} catch (Exception ex) {
}
}
};
}
并且我正像这样从 mainactivity(在 oncreate 中)开始我的服务,
if (settings.getBoolean("createuploaderdownloadtimer11", true)) {
startService(new Intent(this, AndroidService.class));
settings.edit().putBoolean("createuploaderdownloadtimer11", false).commit();
}
这就是我在清单中声明的方式,
<service android:name=".AndroidService" >
</service>
当我关闭最近的应用程序选项时,我的服务也会关闭!
它应该在网络连接时执行操作,但没有!
我该怎么办?有帮助吗?
提前致谢..
你应该这样写:
settings.edit().putBoolean("createuploaderdownloadtimer11", false).commit();
在 startService 调用之前:
settings.edit().putBoolean("createuploaderdownloadtimer11", false).commit();
startService(new Intent(this, AndroidService.class));
我开发了一个 android 应用程序,其中我将广播接收器与 android 服务集成在一起。我的座右铭是只要设备连接到互联网,它就会执行一些操作。当我安装该应用程序时,它第一次运行良好。但是当我重新打开我的应用程序时,它无法正常工作。我应该在哪里声明我的服务,以便它 运行 无论应用程序打开或关闭或重新打开!无论发生什么,它都应该 运行 在 bg 中始终并在连接互联网时执行某些操作(正如我编程的那样)。我怎样才能做到这一点?下面是我的 android 服务代码
package processteam.ju;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Environment;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import java.util.logging.Handler;
import java.util.logging.LogRecord;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class AndroidService extends Service {
Timer t;
Timer t1;
TimerTask task;
TimerTask task1;
int time = 20;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
startService(new Intent(this, AndroidService.class));
return START_STICKY;
}
@Override
public void onDestroy() {
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
super.onCreate();
this.registerReceiver(this.mConnReceiver,new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
}
public void zipper(){
{
File uploadedimagedirectory = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/JU_Mobile_Files/Uploaded_Images/");
File[] imagecontents = uploadedimagedirectory.listFiles();
if (imagecontents == null) {
Toast.makeText(getApplicationContext(), "uploadedimagedirectory null", Toast.LENGTH_LONG).show();
} else if (imagecontents.length == 0) {
Toast.makeText(getApplicationContext(), "uploadedimagedirectory empty", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), "uploadedimagedirectory 1st line", Toast.LENGTH_LONG).show();
try {
String inputFolderPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/JU_Mobile_Files/Uploaded_Images/";
String outZipPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/JU_Mobile_Files/Images_Media_Zip/images.zip";
Toast.makeText(this, "uploadimg2dp try block" + inputFolderPath + " ---" + outZipPath, Toast.LENGTH_LONG).show();
FileOutputStream fos = new FileOutputStream(outZipPath);
ZipOutputStream zos = new ZipOutputStream(fos);
Toast.makeText(this, "What i get in uploadimg2dp" + inputFolderPath + " <--> " + outZipPath, Toast.LENGTH_LONG).show();
File srcFile = new File(inputFolderPath);
File[] files = srcFile.listFiles();
Toast.makeText(this, "S.no " + srcFile.getName(), Toast.LENGTH_LONG).show();
for (int i = 0; i < files.length; i++) {
byte[] buffer = new byte[1024];
FileInputStream fis = new FileInputStream(files[i]);
zos.putNextEntry(new ZipEntry(files[i].getName()));
int length;
while ((length = fis.read(buffer)) > 0) {
zos.write(buffer, 0, length);
}
zos.closeEntry();
fis.close();
}
zos.close();
} catch (IOException ioe) {
Log.e("", ioe.getMessage());
}
}
File uploadedvideodirectory = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/JU_Mobile_Files/Uploaded_Videos/");
File[] videocontents = uploadedvideodirectory.listFiles();
if (videocontents == null) {
Toast.makeText(getApplicationContext(), "uploadedvideodirectory null", Toast.LENGTH_LONG).show();
} else if (videocontents.length == 0) {
Toast.makeText(getApplicationContext(), "uploadedvideodirectory empty", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), "uploadedvideodirectory executed", Toast.LENGTH_LONG).show();
try {
String inputFolderPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/JU_Mobile_Files/Uploaded_Videos";
String outZipPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/JU_Mobile_Files/Videos_Media_Zip/videos.zip";
Toast.makeText(this, "exv" + inputFolderPath + " ---" + outZipPath, Toast.LENGTH_LONG).show();
FileOutputStream fos = new FileOutputStream(outZipPath);
ZipOutputStream zos = new ZipOutputStream(fos);
Toast.makeText(this, "What i get in zip2 " + inputFolderPath + " <--> " + outZipPath, Toast.LENGTH_LONG).show();
File srcFile = new File(inputFolderPath);
File[] files = srcFile.listFiles();
Log.d("", "Zip directory: " + srcFile.getName());
Toast.makeText(this, "S.no " + srcFile.getName(), Toast.LENGTH_LONG).show();
for (int i = 0; i < files.length; i++) {
Log.d("", "Adding file: " + files[i].getName());
byte[] buffer = new byte[1024];
FileInputStream fis = new FileInputStream(files[i]);
zos.putNextEntry(new ZipEntry(files[i].getName()));
int length;
Log.d("ranjithkpr","8148580586ly");
while ((length = fis.read(buffer)) > 0) {
zos.write(buffer, 0, length);
}
zos.closeEntry();
fis.close();
Log.d("ranjithkpr","8148580586hy");
}
zos.close();
Log.d("ranjithkpr","8148580586yt");
} catch (IOException ioe) {
Log.e("", ioe.getMessage());
}
}
Log.d("ranjithkpr","8148580586w");
uploadmedia2dp();
Log.d("ranjithkpr","8148580586y");
}
}
public void startTimer() {
Log.d("Download123456", "Starts123456");
t = new Timer();
task = new TimerTask() {
@Override
public void run() {
DBUploaderDownloader mydb = new DBUploaderDownloader(getApplicationContext(), 0);
//mydb.execute("1");
}
};
SimpleDateFormat currenthour = new SimpleDateFormat("HH");
SimpleDateFormat currentime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
SimpleDateFormat currentdate = new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = Calendar.getInstance();
cal.add(Calendar.HOUR, 1);
int hours = Integer.parseInt(currenthour.format(cal.getTime()));
String target = currentdate.format(new Date());
for (int i = 0; i < 24; i = i + 1) {
if (hours == i) {
String ss = String.valueOf(i);
if (i < 10) {
ss = "0" + ss;
}
target = target + " " + ss + ":30:00";
break;
}
}
String currentdttime = currentime.format(new Date());
Date targetedtime = null;
try {
targetedtime = currentime.parse(target);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
long diff = targetedtime.getTime() - (new Date()).getTime();
//t.scheduleAtFixedRate(task, 100, 20000);
//t.scheduleAtFixedRate(task, diff, 3600000);
t.scheduleAtFixedRate(task, 100, 1800000);
}
public void downloaddb() {
}
public void startUploadTimer() {
Log.d("Upload123456", "Starts123456");
t1 = new Timer();
task1 = new TimerTask() {
@Override
public void run() {
/* DBUploaderDownloader mydb = new DBUploaderDownloader(getApplicationContext(), 0);
mydb.execute("2");*/
downloaddb();
}
};
SimpleDateFormat currenthour = new SimpleDateFormat("HH");
SimpleDateFormat currentime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
SimpleDateFormat currentdate = new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = Calendar.getInstance();
cal.add(Calendar.HOUR, 1);
int hours = Integer.parseInt(currenthour.format(cal.getTime()));
String target = currentdate.format(new Date());
for (int i = 0; i < 24; i = i + 1) {
if (hours == i) {
String ss = String.valueOf(i);
if (i < 10) {
ss = "0" + ss;
}
target = target + " " + ss + ":00:00";
break;
}
}
String currentdttime = currentime.format(new Date());
Date targetedtime = null;
try {
targetedtime = currentime.parse(target);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
long diff = targetedtime.getTime() - (new Date()).getTime();
Log.d("1234","diff="+String.valueOf(diff));
//t1.scheduleAtFixedRate(task1, 100, 360000);
//t1.scheduleAtFixedRate(task1, diff, 3600000);
//t1.scheduleAtFixedRate(task1, 100, 1800000);
t1.scheduleAtFixedRate(task1, 100, 1200000);
}
public void uploadmedia2dp(){
Log.d("ranjithkpr","8148580586");
/*Intent dialogIntent = new Intent(getBaseContext(), DropBoxActivity.class);
dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplication().startActivity(dialogIntent);*/
Intent in=new Intent().setClass(AndroidService.this,DropBoxActivity.class);
in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(in);
// Toast.makeText(getApplicationContext(),"uploadmedia2dp executed",Toast.LENGTH_LONG).show();
Log.d("ranjithkpr","814858058678");
}
private BroadcastReceiver mConnReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
try {
boolean noConnectivity = intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
String reason = intent.getStringExtra(ConnectivityManager.EXTRA_REASON);
boolean isFailover = intent.getBooleanExtra(ConnectivityManager.EXTRA_IS_FAILOVER, false);
NetworkInfo currentNetworkInfo = (NetworkInfo) intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
NetworkInfo otherNetworkInfo = (NetworkInfo) intent.getParcelableExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO);
if (currentNetworkInfo.isConnected()) {
if (t != null) {
t.cancel();
}
if (t1 != null) {
t1.cancel();
}
Toast.makeText(getApplicationContext(),"Connected",Toast.LENGTH_LONG).show();
//startTimer();
//startUploadTimer();
//uploadvideo2dp();
//uploadmedia2dp();
//uploadimage2dp();
zipper();
RecursiveFileObserver recursiveFileObserver = new RecursiveFileObserver(Environment.getExternalStorageDirectory().getAbsolutePath() + "/JU_Mobile_Files/Uploaded_Videos/");
recursiveFileObserver.startWatching();
Log.d("ranjithkpr","8148580586ggy");
RecursiveFileObserver recursiveFileObserver2 = new RecursiveFileObserver(Environment.getExternalStorageDirectory().getAbsolutePath() + "/JU_Mobile_Files/Uploaded_Images/");
recursiveFileObserver2.startWatching();
Log.d("ranjithkpr","8148580586yre");
} else {
if (t != null) {
t.cancel();
}
if (t1 != null) {
t1.cancel();
}
Toast.makeText(getApplicationContext(),"DisConnected",Toast.LENGTH_LONG).show();
}
} catch (Exception ex) {
}
}
};
}
并且我正像这样从 mainactivity(在 oncreate 中)开始我的服务,
if (settings.getBoolean("createuploaderdownloadtimer11", true)) {
startService(new Intent(this, AndroidService.class));
settings.edit().putBoolean("createuploaderdownloadtimer11", false).commit();
}
这就是我在清单中声明的方式,
<service android:name=".AndroidService" >
</service>
当我关闭最近的应用程序选项时,我的服务也会关闭!
它应该在网络连接时执行操作,但没有!
我该怎么办?有帮助吗?
提前致谢..
你应该这样写:
settings.edit().putBoolean("createuploaderdownloadtimer11", false).commit();
在 startService 调用之前:
settings.edit().putBoolean("createuploaderdownloadtimer11", false).commit();
startService(new Intent(this, AndroidService.class));