2012-02-10 124 views
-1

我想从一个需要“android.intent.action.BOOT_COMPLETED”的广播接收器启动服务。当我在eclipse上点击应用程序并启动时,它运行良好。但是当我从它已经安装的模拟器启动它时,应用程序崩溃。以下是源代码。当我使用广播接收器时,应用程序崩溃

AndroidManifest

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.example.android.newsreader" 
android:versionCode="1" 
android:versionName="1.0" > 
<uses-sdk 
    android:minSdkVersion="13" 
    android:targetSdkVersion="15" /> 
<supports-screens 
    android:largeScreens="true" 
    android:normalScreens="true" 
    android:smallScreens="true" 
    android:xlargeScreens="true" /> 
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> 
<application 
    android:icon="@drawable/icon" 
    android:label="@string/app_name" 
    android:logo="@drawable/logo" 
    android:theme="@style/NewsReaderStyle" > 

    <receiver 
     android:name=".StartupIntentReceiver" 
     android:enabled="true" 
     android:permission="android.permission.RECEIVE_BOOT_COMPLETED" > 
     <intent-filter> 
      <action android:name="android.intent.action.BOOT_COMPLETED" /> 
      <category android:name="android.intent.category.DEFAULT" /> 
     </intent-filter> 
    </receiver> 
    <service android:name=".LoadFeedsService" ></service> 

    <activity 
     android:name=".NewsReaderActivity" 
     android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
    <activity 
     android:name=".ArticleActivity" 
     android:theme="@style/NewsReaderStyle_NoActionBar" /> 
</application> 
<uses-permission android:name="android.permission.INTERNET" /> 
</manifest>* 

和我广播reciever事先

*package com.example.android.newsreader; 
import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.util.Log; 
public class StartupIntentReceiver extends BroadcastReceiver{ 
@Override 
public void onReceive(Context context, Intent intent) { 
    Log.i("reciever", "recieved intent"+ intent); 
    Intent serviceIntent = new Intent(context, LoadFeedsService.class); 

    context.startService(serviceIntent); 
} 
}* 

感谢。

+1

崩溃? Logcat输出中的崩溃报告是什么? – 2012-02-10 05:27:35

+0

其实..有更多的问题在那里。我的后台服务是从互联网获取数据,可能互联网连接直到收到广播时才可用... – Ahmad 2012-02-10 07:34:29

回答

2

修改您的接收器代码如下并尝试。

<receiver 
     android:name=".StartupIntentReceiver" 
     <intent-filter> 
      <action android:name="android.intent.action.BOOT_COMPLETED" /> 
      <category android:name="android.intent.category.HOME" /> 
     </intent-filter> 
    </receiver> 

如果上述修改不起作用修改服务也如下,并尝试。

<service 
    android:name=".LoadFeedsService"> 
    <intent-filter > 
     <action android:name="testapp.BACKGROUND_SERVICE" />     
    </intent-filter>    
</service> 

在接收方的java代码中修改如下。

Intent serviceIntent = new Intent("testapp.BACKGROUND_SERVICE"); 

我希望它可以帮助你。

+0

非常好,干净。 5星给你.. – Ahmad 2012-02-10 07:23:18

+0

@Ahmad发生了什么人? – 2012-02-10 09:23:52

+0

服务在启动时启动。但实际上,Iw从互联网获取数据,并且在接收到广播时可能无法连接。我正在想办法在我的服务从互联网获取数据之前获得连接.. – Ahmad 2012-02-10 11:15:36

0

当您的应用程序已经安装在模拟器中,并且您尝试再次在您的emaulator中安装该应用程序时,安装过程中全新的安装过度。此过程有时确实会导致一些问题,如应用程序崩溃,因为新安装不是干净的构建,而是覆盖的构建。所以一定要记住,只要你在代码中做了一些大的改动,最好先卸载已经存在的应用程序,然后再安装一次。

+0

这不是问题。请参阅当我重新安装应用程序时,它会运行,因为BOOT事件在安装之前已经播出并且旁路器被旁路。但是当我从模拟器启动它时,它捕捉到广播事件并崩溃。我猜想有意向和广播接收者存在一些问题。我在广播中编写了日志,即使Logcat上没有显示日志,也就是说,广告接收者的代码没有被执行 – Ahmad 2012-02-10 06:32:54

相关问题