警报和广播接收器无法正常工作

时间:2015-09-03 10:13:09

标签: android broadcastreceiver alarm

我有这个Android应用程序用于警报和广播,但它无法正常工作。 我没有收到任何通知。所以我不确定警报是否也能正常工作。

请告诉我哪里出错了。

的AndroidManifest.xml

<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>
    </activity>
</application>

AlarmMainActivity

public class AlarmMainActivity extends ActionBarActivity {

    private static final String TAG = "AlarmMainActivity";

    @Override
    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");
    }
}

1 个答案:

答案 0 :(得分:-1)

创建BroadcastReceiver时要做的第一件事就是注册它!

这可以在<application />内的AndroidManifest.xml内静态完成  标记,与您的标记在同一级别 找到<activity />标记。

<receiver
  android:name="fill.your.package.name.AlarmReceiver">

  <intent-filter>
    <action android:name="MY_ACTION_STRING" />
  </intent-filter>
</receiver>

这也可以在运行时完成。也许有些情况下您需要动态注册接收器,因为您不需要在应用程序的整个生命周期内停留的人。

例如,在活动中:

BroadcastReceiver myDynamicReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        // does things 
        ...
        // maybe when done you won't need it anymore so ..
        unregisterReceiver(this);
    }
}
String MY_STRING_ACTION = "ACTION_FOR_RECEIVER";
IntentFilter filter = new IntentFilter(MY_STRING_ACTION);

registerReceiver(myDynamicReceiver, filter);

记住你必须在某处取消注册!