为什么我的代码中的 getIntent() 错误红线?
Why getIntent() error red line in my code?
我想将可打包数据从 MainActivity 发送到 LocationService。但是当我想在 LocationService 上检索数据并键入此代码行时。但最终出现错误红线。
我试过这个,但我想在其他方法上使用该对象,结果该对象变为空。
Intent intent = getIntent();
我希望你们能提供帮助,也许还有其他建议。谢谢
因为您正在尝试从服务调用 getIntent,所以它不是服务 class 的方法,您需要在调用它之前参考 Activity class
activity.getIntent()
已编辑:
现在要将数据从 activity 发送到服务,您可以实现一个接口
声明接口
public interface MyIntentListener {
public void call();
}
现在在您的服务中实现此接口,例如
class yourService extends ... implements MyIntentListener {
// other stuff
@override
public void call() {
}
}
现在在 Activity 初始化服务的地方,你必须有它的引用,这样你就可以在 activity
的任何地方调用
service.call()
您也可以使用getIntent().getParcelableExtra(EXTRA_INFO);
您不需要在其中创建意向变量 'getIntent()' ..直接获取它即可。
您可以将 IntentService 用于此类工作:
public class LocationService extends IntentService {
public LocationService() {
super("LocationService");
}
@Override
protected void onHandleIntent(@Nullable Intent intent) {
}
@Override
public int onStartCommand(@Nullable Intent intent, int flags, int startId) {
//Get data from intent here.
}
}
启动并使用 IntentService:
Intent serviceIntent = new Intent(context, LocationService.class);
startService(serviceIntent);
来自 activity。您可以将任何类型的数据传递到 serviceIntent 对象中。
您可以使用 intent 传递数据
Intent serviceIntent= new Intent(Activity_Namethis,Service_Name.class);
serviceIntent.intent.putExtra(EXTRA_KEY, YOUR_OBJECT);
startService(serviceIntent);
从服务中检索数据
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
YOUR_CLASS _yourClassObject = intent.getParcelableExtra(EXTRA_KEY);
return "RETURN_FLAG";
}
您还可以捆绑所有数据并添加 enter code here
您的意图
我想将可打包数据从 MainActivity 发送到 LocationService。但是当我想在 LocationService 上检索数据并键入此代码行时。但最终出现错误红线。
我试过这个
Intent intent = getIntent();
我希望你们能提供帮助,也许还有其他建议。谢谢
因为您正在尝试从服务调用 getIntent,所以它不是服务 class 的方法,您需要在调用它之前参考 Activity class
activity.getIntent()
已编辑:
现在要将数据从 activity 发送到服务,您可以实现一个接口
声明接口
public interface MyIntentListener {
public void call();
}
现在在您的服务中实现此接口,例如
class yourService extends ... implements MyIntentListener {
// other stuff
@override
public void call() {
}
}
现在在 Activity 初始化服务的地方,你必须有它的引用,这样你就可以在 activity
的任何地方调用 service.call()
您也可以使用getIntent().getParcelableExtra(EXTRA_INFO);
您不需要在其中创建意向变量 'getIntent()' ..直接获取它即可。
您可以将 IntentService 用于此类工作:
public class LocationService extends IntentService {
public LocationService() {
super("LocationService");
}
@Override
protected void onHandleIntent(@Nullable Intent intent) {
}
@Override
public int onStartCommand(@Nullable Intent intent, int flags, int startId) {
//Get data from intent here.
}
}
启动并使用 IntentService:
Intent serviceIntent = new Intent(context, LocationService.class);
startService(serviceIntent);
来自 activity。您可以将任何类型的数据传递到 serviceIntent 对象中。
您可以使用 intent 传递数据
Intent serviceIntent= new Intent(Activity_Namethis,Service_Name.class);
serviceIntent.intent.putExtra(EXTRA_KEY, YOUR_OBJECT);
startService(serviceIntent);
从服务中检索数据
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
YOUR_CLASS _yourClassObject = intent.getParcelableExtra(EXTRA_KEY);
return "RETURN_FLAG";
}
您还可以捆绑所有数据并添加 enter code here
您的意图