当我使用广播接收器时应用程序崩溃

发布于 2025-01-04 05:25:32 字数 2470 浏览 0 评论 0原文

我想从广播接收器启动一项服务,该服务采用“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夏天碎花小短裙 2025-01-11 05:25:32

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

    <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");

我希望它可以帮助你。

Modify your receiver code as below and try.

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

If the above modification is not working modify service also as below and try.

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

In java code of receiver modify as below.

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

I hope it may help you.

热风软妹 2025-01-11 05:25:32

当您的应用程序已安装在模拟器中并且您尝试在模拟器中再次安装该应用程序时,安装将被全新安装覆盖。此过程有时确实会导致一些问题,例如应用程序崩溃,因为新安装不是干净的版本,而是覆盖的版本。因此,请始终记住,每当您对代码进行重大更改时,最好先卸载现有的应用程序,然后再次安装。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文