Intent 在服务中一直为 Null
Intent is Null all the time in service
我有两个应用程序 A 和 B.In 应用程序 B 我有一个服务,我可以从应用程序 A 运行 它。我想发送数据到应用程序 B,但我的意图始终为空!
我 运行 使用此代码从应用程序 A 获得应用程序 B 的服务:
try {
String packageName = "app_B_package";
String appService = packageName + ".activity.InternetService";
Intent start = new Intent();
start.setComponent(new ComponentName(packageName, appService));
start.putExtra("LAUNCHER_COMMAND_CLOSE" , true);
G.context.startService(start);
} catch (Exception e) {
e.printStackTrace();
}
但是当应用程序 B 的服务将 运行 时,意图为空。这是应用程序 B 中服务的 onStart
:
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
Log.i("LOGO_OFFICE_IN", "onStart");
if (intent != null) {
if (intent.getExtras().getBoolean("LAUNCHER_COMMAND_CLOSE")) {
Tools.clearApplicationData(InternetService.this);
new AppStatus(InternetService.this).isAppRunning(getPackageName(), true);
}
}
}
为什么我的意图一直都是空的?我找不到了。
感谢您的帮助。
看起来您的服务类型是即发即弃 - 它做的事情很快,应该立即退出,因为它已完成。正确吗?
1。不要离开你的闲置服务运行
文档说
If a component starts the service by calling startService()
(which results in a call to onStartCommand()
), the service continues to run until it stops itself with stopSelf()
or another component stops it by calling stopService()
.
所以在你的工作量完成后调用 stopSelf()
。
当您的服务不可用时 运行 无需重新启动。
2。使用正确的启动方式
除非您停止它,否则您的服务默认情况下会在被系统终止后自动重新启动(因为系统需要资源)。默认模式称为 START_STICKY
并执行此操作:
This mode makes sense for things that will be explicitly started and stopped to run for arbitrary periods of time, such as a service performing background music playback.
由于您的服务是一项快速的一次性工作,因此稍后在任意时间重新启动它没有任何意义。
要让 Android 知道,您应该 return START_NOT_STICKY
来自 onStartCommand
。
3。使用当前 API
不要使用 onStart
,它在 9 年前就被弃用了。它不支持上述启动模式。改为实施 onStartCommand
。您的服务将如下所示:
@Override
public void onStartCommand(Intent intent, int flags, int startId) {
// No super call.
Log.i("LOGO_OFFICE_IN", "onStart");
// Intent cannot be null.
if (intent.getExtras().getBoolean("LAUNCHER_COMMAND_CLOSE")) {
Tools.clearApplicationData(InternetService.this);
new AppStatus(InternetService.this).isAppRunning(getPackageName(), true);
}
stopSelf(); // Work is done, stop service.
return START_NOT_STICKY; // Don't restart if killed.
}
现在想起来,只有第一步是绝对必要的。无论如何,养成使用当前 API 的习惯,并找出它是如何工作的。
我有两个应用程序 A 和 B.In 应用程序 B 我有一个服务,我可以从应用程序 A 运行 它。我想发送数据到应用程序 B,但我的意图始终为空!
我 运行 使用此代码从应用程序 A 获得应用程序 B 的服务:
try {
String packageName = "app_B_package";
String appService = packageName + ".activity.InternetService";
Intent start = new Intent();
start.setComponent(new ComponentName(packageName, appService));
start.putExtra("LAUNCHER_COMMAND_CLOSE" , true);
G.context.startService(start);
} catch (Exception e) {
e.printStackTrace();
}
但是当应用程序 B 的服务将 运行 时,意图为空。这是应用程序 B 中服务的 onStart
:
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
Log.i("LOGO_OFFICE_IN", "onStart");
if (intent != null) {
if (intent.getExtras().getBoolean("LAUNCHER_COMMAND_CLOSE")) {
Tools.clearApplicationData(InternetService.this);
new AppStatus(InternetService.this).isAppRunning(getPackageName(), true);
}
}
}
为什么我的意图一直都是空的?我找不到了。
感谢您的帮助。
看起来您的服务类型是即发即弃 - 它做的事情很快,应该立即退出,因为它已完成。正确吗?
1。不要离开你的闲置服务运行
文档说
If a component starts the service by calling
startService()
(which results in a call toonStartCommand()
), the service continues to run until it stops itself withstopSelf()
or another component stops it by callingstopService()
.
所以在你的工作量完成后调用 stopSelf()
。
当您的服务不可用时 运行 无需重新启动。
2。使用正确的启动方式
除非您停止它,否则您的服务默认情况下会在被系统终止后自动重新启动(因为系统需要资源)。默认模式称为 START_STICKY
并执行此操作:
This mode makes sense for things that will be explicitly started and stopped to run for arbitrary periods of time, such as a service performing background music playback.
由于您的服务是一项快速的一次性工作,因此稍后在任意时间重新启动它没有任何意义。
要让 Android 知道,您应该 return START_NOT_STICKY
来自 onStartCommand
。
3。使用当前 API
不要使用 onStart
,它在 9 年前就被弃用了。它不支持上述启动模式。改为实施 onStartCommand
。您的服务将如下所示:
@Override
public void onStartCommand(Intent intent, int flags, int startId) {
// No super call.
Log.i("LOGO_OFFICE_IN", "onStart");
// Intent cannot be null.
if (intent.getExtras().getBoolean("LAUNCHER_COMMAND_CLOSE")) {
Tools.clearApplicationData(InternetService.this);
new AppStatus(InternetService.this).isAppRunning(getPackageName(), true);
}
stopSelf(); // Work is done, stop service.
return START_NOT_STICKY; // Don't restart if killed.
}
现在想起来,只有第一步是绝对必要的。无论如何,养成使用当前 API 的习惯,并找出它是如何工作的。