我有一个令人费解的问题,我认为由于愚蠢的疏忽,这将很容易解决。
我的主要任务是将图像和录音文件上传到我服务器上的某个位置。我是通过FTP完成的。
我遇到第4步的问题。 循环只上传我的两个文件,然后突然停止。在上传第二个文件时,手机进入睡眠模式。没有通知。
我试过以下
有人可以建议前进的方向吗? 这是我的代码
这是MainActivity.class中的代码
Intent service = new Intent(getApplicationContext(), UploadMedia.class);
service.putExtra("imageFileName", "/Folder/uploadtheseimagefiles.txt");
service.putExtra("audioFileName", "/Folder/uploadtheseaudiofiles.txt");
startService(service);
这是我的IntentService代码
protected void onHandleIntent(Intent intent) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
final String imageFileName = intent.getStringExtra("imageFileName");
final String audioFileName = intent.getStringExtra("audioFileName");
new Thread(new Runnable() {
@Override
public void run() {
String serverAddress = "server address here";
String userID = "user name here";
String password = "password here";
ArrayList<String> confirmed = new ArrayList<String>();
//1. Upload the image files
if (imageFileName.length() != 0) {
File uploadContents = new File(imageFileName);
if (uploadContents.exists()) {
try {
//Read the entries to be uploaded
InputStream ststr = new FileInputStream(uploadContents);
BufferedReader stbr = new BufferedReader(new InputStreamReader(ststr));
String line = "";
upData.clear();
while ((line = stbr.readLine ()) != null) {
upData.add(line.trim());
}
stbr.close();
ststr.close();
//Upload the entries
confirmed.clear();
for(int i = 0; i < upData.size(); i++) {
if (new UploadViaFTP().ftpUpload(upData.get(i).toString(), serverAddress, userID, password)) {
confirmed.add(upData.get(i).toString());
}
}
//Check the uploaded filenames against the ones that were to be uploaded.
ArrayList<String> remaining = new ArrayList<String>();
for (int i = 0; i < upData.size(); i++) {
if (!confirmed.contains(upData.get(i)))
remaining.add(upData.get(i));
}
//If there are any remaining, write them back into the file.
if (remaining.size() != 0) {
try {
FileWriter fw = new FileWriter(imageFileName);
for (int i = 0; i < remaining.size(); i++) {
fw.write(remaining.get(i));
fw.append("\n");
}
fw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else { //All files have been uploaded
File delFile = new File(imageFileName);
if (delFile.exists())
delFile.delete();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//2. Upload the audio files
if (audioFileName.length() != 0) {
File uploadContents = new File(audioFileName);
if (uploadContents.exists()) {
try {
//Read the entries to be uploaded
InputStream ststr = new FileInputStream(uploadContents);
BufferedReader stbr = new BufferedReader(new InputStreamReader(ststr));
String line = "";
upData.clear();
while ((line = stbr.readLine ()) != null) {
upData.add(line.trim());
}
stbr.close();
ststr.close();
//Upload the entries
confirmed.clear();
for(int i = 0; i < upData.size(); i++) {
if (new UploadViaFTP().ftpUpload(upData.get(i).toString(), serverAddress, userID, password)) {
confirmed.add(upData.get(i).toString());
}
}
//Check the uploaded filenames against the ones that were to be uploaded.
ArrayList<String> remaining = new ArrayList<String>();
for (int i = 0; i < upData.size(); i++) {
if (!confirmed.contains(upData.get(i)))
remaining.add(upData.get(i));
}
//If there are any remaining, write them back into the file.
if (remaining.size() != 0) {
try {
FileWriter fw = new FileWriter(audioFileName);
for (int i = 0; i < remaining.size(); i++) {
fw.write(remaining.get(i));
fw.append("\n");
}
fw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else { //All files have been uploaded
File delFile = new File(audioFileName);
if (delFile.exists())
delFile.delete();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
stopSelf();
//Final check before notification.
File checkFile = new File("/Folder/uploadtheseimagefiles.txt");
if (!checkFile.exists()) {
checkFile = new File("/Folder/uploadtheseaudiofiles.txt");
if (!checkFile.exists()) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext())
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Something")
.setContentText("Uploading of media complete.");
Intent notificationIntent = new Intent(getApplicationContext(), MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(contentIntent);
// Add as notification
NotificationManager manager = (NotificationManager) getSystemService(getApplication().NOTIFICATION_SERVICE);
manager.notify(0, builder.build());
}
}
}
}).start();
}
Manifest.xml文件
<service
android:name=".UploadMedia"
android:exported="true"
android:enabled="true"/>
新信息: 根据我的测试用例,失败总是发生在第二个文件中。第一个图像大小约为900KB,第二个图像大小约为2MB。 在调试时,我无法获得第二个文件的FTPUpload状态。但调试器会停止第一个文件,我可以继续。
新信息:第2部分: 它可能是相关的。我正在使用asynctask将一些数据更新到我服务器上的数据库。成功返回后,它会整理所有需要上载的文件名。这个整理的数据是我在Intent中传递给服务的。
另一个忏悔。我有这个确切的过程在我的另一个应用程序中运行和工作。使用asynctask将数据上载到数据库。在post执行方法中,它调用服务将数据上传到FTP服务器。我已经将清单文件,服务和对服务的调用进行了比较,一切似乎都是一样的。
新信息:第3部分: LOG文件
02-12 10:03:40.521: I/PERSONAL(14962): Calling the service
02-12 10:03:40.525: W/Binder_1(791): type=1400 audit(0.0:20440): avc: denied { ioctl } for path="socket:[743779]" dev="sockfs" ino=743779 ioctlcmd=7704 scontext=u:r:system_server:s0 tcontext=u:r:system_server:s0 tclass=unix_stream_socket permissive=0
02-12 10:03:40.525: W/Binder_1(791): type=1400 audit(0.0:20441): avc: denied { ioctl } for path="socket:[743779]" dev="sockfs" ino=743779 ioctlcmd=7704 scontext=u:r:system_server:s0 tcontext=u:r:system_server:s0 tclass=unix_stream_socket permissive=0
02-12 10:03:40.534: I/PERSONAL(14962): Uploading audio file 0
02-12 10:03:40.585: V/RenderScript(14962): 0xb397e000 Launching thread(s), CPUs 4
02-12 10:03:42.278: I/Finsky(14117): [3310] com.google.android.finsky.d.e.run(1151): Replicating app states via AMAS.
02-12 10:03:42.398: I/Finsky(14117): [3310] com.google.android.finsky.d.c.a(313): Completed 0 account content syncs with 0 successful.
02-12 10:03:42.400: I/Finsky(14117): [1] com.google.android.finsky.services.j.a(148): Installation state replication succeeded.
02-12 10:03:42.434: I/PERSONAL(14962): Uploading audio file 1
02-12 10:03:42.515: W/Binder_F(1876): type=1400 audit(0.0:20442): avc: denied { ioctl } for path="socket:[743796]" dev="sockfs" ino=743796 ioctlcmd=7704 scontext=u:r:system_server:s0 tcontext=u:r:system_server:s0 tclass=unix_stream_socket permissive=0
02-12 10:03:42.515: W/Binder_F(1876): type=1400 audit(0.0:20443): avc: denied { ioctl } for path="socket:[743796]" dev="sockfs" ino=743796 ioctlcmd=7704 scontext=u:r:system_server:s0 tcontext=u:r:system_server:s0 tclass=unix_stream_socket permissive=0
02-12 10:03:43.400: D/audio_hw_primary(201): disable_audio_route: reset and update mixer path: low-latency-playback
02-12 10:03:43.400: D/audio_hw_primary(201): disable_snd_device: snd_device(2: speaker)
02-12 10:03:44.222: I/PERSONAL(14962): Uploading image file 0
02-12 10:03:44.565: W/Binder_5(1372): type=1400 audit(0.0:20444): avc: denied { ioctl } for path="socket:[745643]" dev="sockfs" ino=745643 ioctlcmd=7704 scontext=u:r:system_server:s0 tcontext=u:r:system_server:s0 tclass=unix_stream_socket permissive=0
02-12 10:03:44.565: W/Binder_5(1372): type=1400 audit(0.0:20445): avc: denied { ioctl } for path="socket:[745643]" dev="sockfs" ino=745643 ioctlcmd=7704 scontext=u:r:system_server:s0 tcontext=u:r:system_server:s0 tclass=unix_stream_socket permissive=0
02-12 10:03:46.535: W/Binder_4(1220): type=1400 audit(0.0:20446): avc: denied { ioctl } for path="socket:[745654]" dev="sockfs" ino=745654 ioctlcmd=7704 scontext=u:r:system_server:s0 tcontext=u:r:system_server:s0 tclass=unix_stream_socket permissive=0
02-12 10:03:46.535: W/Binder_4(1220): type=1400 audit(0.0:20447): avc: denied { ioctl } for path="socket:[745654]" dev="sockfs" ino=745654 ioctlcmd=7704 scontext=u:r:system_server:s0 tcontext=u:r:system_server:s0 tclass=unix_stream_socket permissive=0
02-12 10:03:59.995: W/Binder_6(1388): type=1400 audit(0.0:20448): avc: denied { ioctl } for path="socket:[743809]" dev="sockfs" ino=743809 ioctlcmd=7704 scontext=u:r:system_server:s0 tcontext=u:r:system_server:s0 tclass=unix_stream_socket permissive=0
02-12 10:03:59.995: W/Binder_6(1388): type=1400 audit(0.0:20449): avc: denied { ioctl } for path="socket:[743809]" dev="sockfs" ino=743809 ioctlcmd=7704 scontext=u:r:system_server:s0 tcontext=u:r:system_server:s0 tclass=unix_stream_socket permissive=0
02-12 10:04:04.787: I/DeviceStateChecker(1718): screenOn: true, isCharging: true
02-12 10:04:29.560: D/NetlinkSocketObserver(781): NeighborEvent{elapsedMs=152656259, fe80::86c9:b2ff:fe6c:5ce9, [84C9B26C5CE9], RTM_NEWNEIGH, NUD_PROBE}
02-12 10:04:29.705: I/PlayCommon(14117): [3292] com.google.android.play.a.g.e(909): Preparing logs for uploading
02-12 10:04:29.733: I/PlayCommon(14117): [3292] com.google.android.play.a.g.a(1049): Connecting to server: https://play.googleapis.com/play/log?format=raw&proto_v2=true
02-12 10:04:29.901: I/PERSONAL(14962): Uploading image file 1
02-12 10:04:29.971: I/PlayCommon(14117): [3315] com.google.android.play.a.g.e(909): Preparing logs for uploading
02-12 10:04:29.972: I/PlayCommon(14117): [3315] com.google.android.play.a.g.e(911): No file ready to send
02-12 10:04:31.635: I/PlayCommon(14117): [3292] com.google.android.play.a.g.a(1127): Successfully uploaded logs.
02-12 10:04:46.450: D/NetlinkSocketObserver(781): NeighborEvent{elapsedMs=152673149, fe80::86c9:b2ff:fe6c:5ce9, [84C9B26C5CE9], RTM_NEWNEIGH, NUD_STALE}
02-12 10:04:58.348: V/GsmInboundSmsHandler(1549): Unable to find carrier package: [], nor systemPackages: []
02-12 10:04:58.363: D/MmsService(1549): getAutoPersisting
02-12 10:04:58.377: I/ActivityManager(781): Start proc 15147:com.google.android.talk/u0a27 for broadcast com.google.android.talk/com.google.android.apps.hangouts.sms.SmsDeliverReceiver
02-12 10:04:59.064: D/BabelGcmRegistration(15147): GcmRegistration: Checking GCM registration
02-12 10:04:59.079: I/Babel_telephony(15147): TeleModule.onApplicationCreate
02-12 10:04:59.089: I/Babel_SMS(15147): MmsConfig: mnc/mcc: 404/86
02-12 10:04:59.090: I/Babel_SMS(15147): MmsConfig.loadMmsSettings
02-12 10:04:59.091: I/Babel_SMS(15147): MmsConfig.loadDeviceMmsSettings from API: userAgent=Nexus5, uaProfUrl=http://gsm.lge.com/html/gsm/Nexus5-M3.xml
02-12 10:04:59.091: I/Babel_SMS(15147): MmsConfig.loadFromDatabase
02-12 10:04:59.105: E/SQLiteLog(15147): (1) no such table: mmsconfig
02-12 10:04:59.107: I/Babel_SMS(15147): MmsConfig: no mmsconfig table android.database.sqlite.SQLiteException: no such table: mmsconfig (code 1): , while compiling: SELECT key, value, type FROM mmsconfig WHERE numeric=?
02-12 10:04:59.107: I/Babel_SMS(15147): MmsConfig.loadFromResources
02-12 10:04:59.118: I/Babel_Prime(15147): wrapCrashReportingIntoUncaughtExceptionHandler
02-12 10:04:59.119: E/Babel_SMS(15147): canonicalizeMccMnc: invalid mccmnc nullnull
02-12 10:04:59.121: I/Babel_SMS(15147): MmsConfig.loadMmsSettings: userAgent=Nexus5, uaProfUrl=http://gsm.lge.com/html/gsm/Nexus5-M3.xml
02-12 10:04:59.126: I/Babel_App(15147): Startup - clean
02-12 10:04:59.128: I/Babel_SMS(15147): ApnsOta: OTA version -1
02-12 10:04:59.134: I/Babel(15147): Invalid account: 3 isEmptyName: true nameEqualsGaiaId: false
02-12 10:04:59.169: I/Babel_Prime(15147): isMemoryEnabled=false
02-12 10:04:59.169: I/Babel_Prime(15147): isTimerEnabled=false
02-12 10:04:59.169: I/Babel_Prime(15147): isCrashCounterEnabled=true
02-12 10:04:59.170: I/Babel_Prime(15147): primesPackageConfigurationsProvider=false
02-12 10:04:59.172: I/Babel(15147): Deleting: false for 3
02-12 10:04:59.174: I/Babel_Prime(15147): startMemoryMonitor
02-12 10:04:59.233: D/Babel(15147): created account premthomas@gmail.com => Redacted-20-chars
02-12 10:04:59.263: W/FortumoInApp(5700): Broadcast is disabled, there is no sense, to countinue.
02-12 10:04:59.274: I/Babel_ConcService(15147): Binding ConcurrentService
02-12 10:04:59.280: I/ActivityManager(781): Killing 13354:com.android.settings/1000 (adj 15): empty #17
02-12 10:04:59.341: D/ActivityManager(781): cleanUpApplicationRecord -- 13354
02-12 10:04:59.367: I/Babel_ConcService(15147): Acquired partial wake lock to keep ConcurrentService alive
02-12 10:04:59.514: W/IcingInternalCorpora(1644): getNumBytesRead when not calculated.
02-12 10:04:59.518: I/Icing(1644): Usage reports 0 indexed 0 rejected 0 imm upload false
02-12 10:04:59.672: W/linker(15147): /data/app/com.google.android.gms-2/lib/arm/libgmscore.so: unused DT entry: type 0x7ffffffd arg 0x795
02-12 10:04:59.689: W/linker(15147): /data/app/com.google.android.gms-2/lib/arm/libconscrypt_gmscore_jni.so: unused DT entry: type 0x1d arg 0xe0
02-12 10:04:59.689: W/linker(15147): /data/app/com.google.android.gms-2/lib/arm/libconscrypt_gmscore_jni.so: unused DT entry: type 0x7ffffffd arg 0x1cb
02-12 10:04:59.697: V/JNIHelp(15147): Registering com/google/android/gms/org/conscrypt/NativeCrypto's 242 native methods...
02-12 10:04:59.712: I/art(15147): Rejecting re-init on previously-failed class java.lang.Class<com.google.android.gms.org.conscrypt.OpenSSLExtendedSessionImpl>
02-12 10:04:59.713: I/art(15147): Rejecting re-init on previously-failed class java.lang.Class<com.google.android.gms.org.conscrypt.OpenSSLExtendedSessionImpl>
02-12 10:04:59.768: I/ProviderInstaller(15147): Installed default security provider GmsCore_OpenSSL
02-12 10:04:59.823: W/VideoCapabilities(15147): Unrecognized profile 2130706433 for video/avc
02-12 10:04:59.848: I/VideoCapabilities(15147): Unsupported profile 4 for video/mp4v-es
02-12 10:04:59.858: W/VideoCapabilities(15147): Unrecognized profile 2130706433 for video/avc
02-12 10:04:59.859: W/VideoCapabilities(15147): Unrecognized profile 2130706433 for video/avc
02-12 10:04:59.862: W/VideoCapabilities(15147): Unrecognized profile 2130706433 for video/avc
02-12 10:04:59.927: D/NuPlayerDriver(201): reset(0xb3876600)
02-12 10:04:59.927: D/NuPlayerDriver(201): notifyListener_l(0xb3876600), (8, 0, 0)
02-12 10:04:59.927: D/NuPlayerDriver(201): notifyResetComplete(0xb3876600)
02-12 10:04:59.942: I/Babel_ConcService(15147): Released partial wake lock as ConcurrentService became idle
02-12 10:05:00.041: D/NuPlayerDriver(201): notifyListener_l(0xb34b8720), (1, 0, 0)
02-12 10:05:00.041: D/MediaPlayer(921): setSubtitleAnchor in MediaPlayer
02-12 10:05:00.044: I/MediaFocusControl(781): AudioFocus requestAudioFocus() from android.media.AudioManager@dd2779c req=3flags=0x0
02-12 10:05:00.045: D/NuPlayerDriver(201): start(0xb34b8720), state is 4, eos is 0
02-12 10:05:00.045: I/GenericSource(201): start
02-12 10:05:00.053: E/OMXNodeInstance(201): setConfig(103:google.vorbis.decoder, ConfigPriority(0x6f800002)) ERROR: Undefined(0x80001001)
02-12 10:05:00.053: I/ACodec(201): codec does not support config priority (err -2147483648)
02-12 10:05:00.054: I/MediaCodec(201): MediaCodec will operate in async mode
02-12 10:05:00.060: D/Babel_RtcImpressions(15147): Type: 1926
02-12 10:05:00.072: D/PhoneStatusBar(921): disable: < expand ICONS* alerts SYSTEM_INFO* back home recent clock search quick_settings >
02-12 10:05:00.075: D/audio_hw_primary(201): out_set_parameters: enter: usecase(1: low-latency-playback) kvpairs: routing=2
02-12 10:05:00.087: D/NuPlayerDriver(201): notifyListener_l(0xb34b8720), (6, 0, 0)
02-12 10:05:00.096: D/audio_hw_primary(201): select_devices: out_snd_device(2: speaker) in_snd_device(0: none)
02-12 10:05:00.096: D/msm8974_platform(201): platform_send_audio_calibration: sending audio calibration for snd_device(2) acdb_id(15)
02-12 10:05:00.096: D/audio_hw_primary(201): enable_snd_device: snd_device(2: speaker)
02-12 10:05:00.099: D/audio_hw_primary(201): enable_audio_route: apply and update mixer path: low-latency-playback
02-12 10:05:00.122: D/CountryDetector(781): The first listener is added
02-12 10:05:00.639: I/Babel(15147): connection state changed from UNKNOWN to CONNECTED
02-12 10:05:00.869: I/NuPlayerDecoder(201): [audio] saw output EOS
02-12 10:05:01.271: D/NuPlayerDriver(201): notifyListener_l(0xb34b8720), (2, 0, 0)
02-12 10:05:01.271: I/MediaFocusControl(781): AudioFocus abandonAudioFocus() from android.media.AudioManager@dd2779c
02-12 10:05:04.252: D/audio_hw_primary(201): disable_audio_route: reset and update mixer path: low-latency-playback
02-12 10:05:04.253: D/audio_hw_primary(201): disable_snd_device: snd_device(2: speaker)
02-12 10:05:04.883: W/IcingInternalCorpora(1644): getNumBytesRead when not calculated.
02-12 10:05:04.915: I/Icing(1644): Usage reports 0 indexed 0 rejected 0 imm upload false
02-12 10:05:05.932: I/Icing(1644): Indexing D2350FC178F0C3C6D9357237E165CCBC64772E44 from com.google.android.gms
02-12 10:05:06.040: I/Icing(1644): Indexing done D2350FC178F0C3C6D9357237E165CCBC64772E44
02-12 10:05:06.057: D/PhoneStatusBar(921): disable: < expand icons* alerts system_info* back home recent clock search quick_settings >
02-12 10:05:14.171: I/Babel_ConcService(15147): Acquired partial wake lock to keep ConcurrentService alive
02-12 10:05:14.182: I/Babel_ConcService(15147): Released partial wake lock as ConcurrentService became idle
02-12 10:05:18.751: I/ProcessStatsService(781): Prepared write state in 3ms
02-12 10:05:18.753: I/ProcessStatsService(781): Prepared write state in 2ms
02-12 10:05:29.137: I/Telecom(781): PhoneAccountRegistrar: SimCallManager queried, returning: null
02-12 10:05:29.143: I/Babel_telephony(15147): TeleModule.updateConnectionManagerRegistration, registration preference changed from false to false
02-12 10:05:29.143: W/Babel(15147): BAM#gBA: invalid account id: -1
02-12 10:05:29.144: W/Babel(15147): BAM#gBA: invalid account id: -1
02-12 10:05:29.144: I/Babel_telephony(15147): TeleModule.updateIncomingCallRegistration, preferred account for incoming calls changed from: null to null
02-12 10:05:29.151: I/Telecom(781): PhoneAccountRegistrar: SimCallManager queried, returning: null
02-12 10:05:49.090: I/PowerManagerService(781): Going to sleep due to screen timeout (uid 1000)...
02-12 10:05:49.175: W/Binder_3(958): type=1400 audit(0.0:20450): avc: denied { ioctl } for path="socket:[743742]" dev="sockfs" ino=743742 ioctlcmd=7704 scontext=u:r:system_server:s0 tcontext=u:r:system_server:s0 tclass=unix_stream_socket permissive=0
02-12 10:05:49.175: W/Binder_3(958): type=1400 audit(0.0:20451): avc: denied { ioctl } for path="socket:[743742]" dev="sockfs" ino=743742 ioctlcmd=7704 scontext=u:r:system_server:s0 tcontext=u:r:system_server:s0 tclass=unix_stream_socket permissive=0
02-12 10:05:49.091: I/PowerManagerService(781): Sleeping (uid 1000)...
02-12 10:05:49.665: V/KeyguardServiceDelegate(781): onScreenTurnedOff()
我会接受任何建议/问题。
答案 0 :(得分:0)
为什么要在onHandleIndent
中创建新主题?这个方法已经从后台线程调用,因此它已经不会妨碍你的UI渲染或其他东西。只需在onHandleIntent
方法中直接运行您的逻辑。试一试
答案 1 :(得分:0)
您不需要调用stopSelf(),因为当队列中没有请求要服务时,IntentService会自行停止。
添加更多日志语句,以便在上传所有文件之前找出它到达stopSelf的原因。