广播接收器不适合我

时间:2015-09-04 05:13:37

标签: android broadcastreceiver

我有 AlarmMainActivity ,它会在闹钟设置的时间内播放意图。接收方程序AlarmReceiver应该捕获此意图并发送通知。从日志中,我可以看到警报正在设置,但是接收器无法启动且无法正常工作。你能看一下下面我的代码吗,请告诉我为什么BroadcastReceiver无效。感谢。

AlarmMainActivity:

   protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_alarm_main);

        Intent alertIntent = new Intent(this, AlarmReceiver.class);
        final PendingIntent pendingIntent = 
           PendingIntent.getBroadcast
          (this,1,alertIntent,PendingIntent.FLAG_UPDATE_CURRENT);

        final Button alarmButton = (Button)findViewById(R.id.alarm_button);
        alarmButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Long alertTime = new 
                GregorianCalendar().getTimeInMillis()+5*1000;
                Log.i(TAG,"Alarm will be sent at : "+ alertTime.toString());

                AlarmManager am = 
                (AlarmManager)getSystemService(Context.ALARM_SERVICE);
                am.set(AlarmManager.RTC_WAKEUP,alertTime,pendingIntent );

                Log.i(TAG, "Alarm is now set");
             }
        });
    }//oncreate

AlarmReceiver:

    public class AlarmReceiver extends BroadcastReceiver{
    private static final String TAG = "AlarmReceiver";
    private String msgTitle, msgText, msgTicker;

    @Override
    public void onReceive(Context context, Intent intent) {

        Log.i(TAG, "Entered AlarmReceiver()");
        msgTitle = "Todays Weather";
        msgText = "Its Sunny and Warm";
        msgTicker="Alert";

        PendingIntent notifyIntent = PendingIntent.getActivity(context,0, new 
         Intent(context, AlarmMainActivity.class),0);

        NotificationCompat.Builder mBuilder = new 
         NotificationCompat.Builder(context).
                setSmallIcon(R.drawable.weather_image).
                setTicker(msgTicker).
                setContentTitle(msgTitle).
                setContentText(msgText).
                setDefaults(NotificationCompat.DEFAULT_SOUND).
                setAutoCancel(true).
                setContentIntent(notifyIntent);

        NotificationManager notificationManager = 
           (NotificationManager)context.getSystemService
           (Context.NOTIFICATION_SERVICE);
        notificationManager.notify(1,mBuilder.build());
        Log.i(TAG,"Notification Sent");
    }

AndroidManifest:

    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.thirdlaw.alertalarm" >

    <uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme"
        android:debuggable="true">
        <activity
            android:name=".AlarmMainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

            <receiver android:name="com.thirdlaw.alertalarm.AlarmReceiver">
                <intent-filter>
                    <action android:name="MY_ACTION_STRING"/>
                </intent-filter>
            </receiver>
        </activity>
    </application>

   </manifest>

1 个答案:

答案 0 :(得分:1)

<receiver />标记中删除<activity />标记,如:

    <activity
        android:name=".AlarmMainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

    <receiver android:name="com.thirdlaw.alertalarm.AlarmReceiver">
        <intent-filter>
            <action android:name="MY_ACTION_STRING"/>
        </intent-filter>
    </receiver>