mst*_*188 4 android android-intent android-service android-activity
我试图在我的意图中传递我的活动指针,但我不知道如何检索它.
我有一个调用活动(MyActivity),其中包含以下代码片段:
Intent myServiceIntent = new Intent(this, myService.class);
startService(myServiceIntent);
Run Code Online (Sandbox Code Playgroud)
在我的服务类中,我想要检索调用活动.像这样的东西:
private Activity CurrentActivity = null;
public int onStartCommand(Intent intent, int flags, int startId)
{
CurrentActivity = (CurrentActivity) intent.getClass();
return super.onStartCommand(intent, flags, startId);
}
Run Code Online (Sandbox Code Playgroud)
这不起作用,但我也不知道是什么.我也找不到将活动作为参数的putExtra.那我该怎么做呢?
在第一个活动中采用这样的静态活动变量,
public static Activity activity;
Run Code Online (Sandbox Code Playgroud)
在onCreate做这个.
activity = this;
Run Code Online (Sandbox Code Playgroud)
然后在第二个活动中这样做,
Activity activity = (your activity name).activity;
Run Code Online (Sandbox Code Playgroud)
根据我的小知识,这是最好的解决方案.只需传递变量的代码/值,就像这样.
Intent intent = new Intent();
intent.putExtra("someValue", "data");
Run Code Online (Sandbox Code Playgroud)
在第二个活动中检查值,如果值是您所需的值,则获取所需的活动实例.我希望这能解决您的问题,如果愿意,请告诉我.
| 归档时间: |
|
| 查看次数: |
8395 次 |
| 最近记录: |