繁体   English   中英

Android BroadcastReceiver无法正常工作(包括错误)

[英]Android BroadcastReceiver not working (error included)

在我的主要活动中,我正在执行以下操作:

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

这是我的接收者

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();
    }
}

}

这是我的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>

尝试打开MorningReceiver时出现此错误:

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

有人知道我在这里说错了吗? 还是我做错了什么?

您已将MorningReceiver声明为Receiver ,并试图将其作为Service进行访问。

系统正在尝试查找名为MorningReceiverService并失败,因此出现错误。

可以作为接收者访问它,也可以将其更改为服务。

如果您以接收者的身份访问它,请使用以下命令:

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

代替这个:

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

请参考本教程以获得广播接收器的良好概述:

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

采用

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

代替这个

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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