当我使用广播接收器时应用程序崩溃
我想从广播接收器启动一项服务,该服务采用“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>*
和我的 BroadCast 接收器
*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);
}
}*
提前致谢。
I wanted to start a service from a Broadcast reciever which takes "android.intent.action.BOOT_COMPLETED". When I write click on the Application in eclipse and launch it It runs well. But when I launch it from emulator on which it has already been installed, the application crashes. The following are the source codes.
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>*
and my BroadCast reciever is
*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);
}
}*
Thanks in Advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如下修改您的接收器代码并尝试。
如果上述修改不起作用,也按如下方式修改服务并尝试。
在接收器的java代码中修改如下。
我希望它可以帮助你。
Modify your receiver code as below and try.
If the above modification is not working modify service also as below and try.
In java code of receiver modify as below.
I hope it may help you.
当您的应用程序已安装在模拟器中并且您尝试在模拟器中再次安装该应用程序时,安装将被全新安装覆盖。此过程有时确实会导致一些问题,例如应用程序崩溃,因为新安装不是干净的版本,而是覆盖的版本。因此,请始终记住,每当您对代码进行重大更改时,最好先卸载现有的应用程序,然后再次安装。
When your app is already installed in the emulator and u r trying to install the app again in your emaulator the installation is over ridden with the fresh installation. This procedure sometimes does cause some issues like app crashes as the new installation is not a clean build it's a overridden build. So always remember that whenever u make some big change in your code it's always better to first uninstall already existing app and then install it again.