简体   繁体   中英

Android BroadcastReceiver not working (error included)

In my main activity, I am executing this:

        Calendar calendar = Calendar.getInstance();

        // 9:45 PM 
        calendar.set(Calendar.HOUR_OF_DAY, 21);
        calendar.set(Calendar.MINUTE, 45);
        calendar.set(Calendar.SECOND, 0);
        AlarmManager am = (AlarmManager) this.getApplicationContext().getSystemService(Context.ALARM_SERVICE);
        PendingIntent pi = PendingIntent.getService(this.getApplicationContext(), 0, new Intent(this.getApplicationContext(), MorningReceiver.class), PendingIntent.FLAG_UPDATE_CURRENT);
        am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pi);

        Toast msg = Toast.makeText(NotificationScanner.this,
                "Scheduled: " + calendar.getTime(), Toast.LENGTH_LONG);
        msg.show();

And this is my receiver

public class MorningReceiver extends BroadcastReceiver { 
@Override
public void onReceive(Context context, Intent intent) {
    MediaPlayer mMediaPlayer = new MediaPlayer();
    Uri alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM); 
    try {
        mMediaPlayer.setDataSource(context, alert);
        final AudioManager audioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
        if (audioManager.getStreamVolume(AudioManager.STREAM_ALARM) != 0) {
            mMediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
            mMediaPlayer.start();
            PackageManager packageManager = context.getPackageManager();
            Intent skype = packageManager.getLaunchIntentForPackage("com.skype.raider");
            context.startActivity(skype);
        }
    }catch(Exception e){
        Toast msg = Toast.makeText(context,
                "Exception thrown", Toast.LENGTH_LONG);
        msg.show();
    }
}

}

And this is my AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.wayward.skype.wizard"
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=".SkypeActivity"
        android:label="@string/title_activity_skype" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <service android:name="com.wayward.service.NotificationScanner" >
        <intent-filter>
            <action android:name="android.accessibilityservice.AccessibilityService" />
        </intent-filter>
    </service>
    <receiver android:name="com.wayward.service.MorningReceiver" >
    </receiver>
</application>

I am getting this error when it tries to open MorningReceiver:

Unable to start service Intent { flg=0x4 cmp=com.wayward.skype.wizard/com.wayward.service.MorningReceiver (has extras) }: not found

Anyone know what I'm declaring wrong here? Or what I'm doing wrong?

You have declared MorningReceiver as a Receiver and are trying to access it as a Service .

the system is trying to find a Service named MorningReceiver and failing, hence the error.

Either access it as a receiver or change it to be a service.

If you are accessing it as a receiver use this:

PendingIntent pi = PendingIntent.getBroadCast(this.getApplicationContext(), 0, new Intent(this.getApplicationContext(), MorningReceiver.class), PendingIntent.FLAG_UPDATE_CURRENT);

instead of this:

PendingIntent pi = PendingIntent.getService(this.getApplicationContext(), 0, new Intent(this.getApplicationContext(), MorningReceiver.class), PendingIntent.FLAG_UPDATE_CURRENT);

Refer this tutorial to get a good overview of broadcast receivers:

http://vogella.com/articles/AndroidBroadcastReceiver/article.html

use

PendingIntent pi = PendingIntent.getBroadCast(this.getApplicationContext(), 0, new Intent(this.getApplicationContext(), MorningReceiver.class), PendingIntent.FLAG_UPDATE_CURRENT);

instead of this

PendingIntent pi = PendingIntent.getService(this.getApplicationContext(), 0, new Intent(this.getApplicationContext(), MorningReceiver.class), PendingIntent.FLAG_UPDATE_CURRENT);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM