我最近遇到了这个问题,并创建了一个解决方案。与隐形活动类似,但想法是创建一个不需要该服务的“加载”活动。这里有一个例子:
在AndriodManifest.xml:
...
<application
... >
<activity
android:name="com.example.LoadingActivity"
android:label="@string/app_name" >
<!-- A do-nothing activity that is used as a placeholder while the service is started. -->
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.MyActivity"
android:label="@string/app_name" >
<!-- The real activity, started by the loading activity. -->
</activity>
<service
android:name="com.example.MyService" >
<!-- The background service, started by the loading activity. -->
</service>
</application>
...
COM /例子/ LoadingActivity.java:
package com.example;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class LoadingActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
super.setContentView(R.layout.activity_loading);
super.startService(new Intent(this, MyService.class)); // listed first so it will start before the activity
Intent intent = new Intent(this, MyActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); // makes it so the 'loading' activity is not kept on the back-stack
super.startActivity(intent);
}
}
的MyActivity.java和MyService.java文件只是标准。您还需要一个activity_loading.xml布局资源,可能只是一个ImageView。
问题是我需要一个服务或一些托管数据的地方,我可以访问的地方。如果我把我的服务在Application类的onCreate()上做了什么,我怎么能从我的活动引用加载的数据? – user1205781 2013-02-21 00:13:35