我试图从y应用程序的主要活动启动IntentService并且它不会启动。我在清单文件中有服务。下面的代码:活动将不会启动服务
MainActivity
public class Home extends Activity {
private LinearLayout kontejner;
IntentFilter intentFilter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
kontejner = (LinearLayout) findViewById(R.id.kontejner);
intentFilter = new IntentFilter();
startService(new Intent(getBaseContext(), HomeService.class));
}
}
服务:
public class HomeService extends IntentService {
public HomeService() {
super("HomeService");
// TODO Auto-generated constructor stub
}
@Override
protected void onHandleIntent(Intent intent) {
Toast.makeText(getBaseContext(), "TEST", Toast.LENGTH_LONG).show();
}
}
清单:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.salefinder"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".Home"
android:label="@string/title_activity_home" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".HomeService" />
</application>
<uses-permission android:name="android.permission.INTERNET"/>
</manifest>
我怎样才能使它发挥作用?
双方的活动和IntentService拥有有效的环境中,如果你replace'发生带'this'的getBaseContext()'? – Sam
什么也没有,只是一个空白的屏幕 –
我从'onHandleIntent()开始了Toast就好了,但是一旦IntentService完成它的任务就停下来,Toast的处理程序就被销毁并泄露了Toast。直到我手动将应用程序从缓存内存中清除时,它才会消失...... _Oops._所以我建议使用Log语句。但是我看不出为什么你应该看到一个LogCat语句。 – Sam