使用Activity类中的Method

时间:2015-03-19 08:35:08

标签: java android android-intent extends

我正在扩展BroadcastReceiver类,但在给定的函数中,我需要使用 getIntent()函数,它是 Activity类的一部分。 我应该如何使用此功能,因为我只能扩展一个类。

public class AlarmReceiver extends BroadcastReceiver {

AudioManager audioControl;
Bundle extras;
int startHour,startMinute;

@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub
    SimpleDateFormat sdf = new SimpleDateFormat("EEEE");
    Date d = new Date();
    String day = sdf.format(d);
    Calendar c = Calendar.getInstance();
    audioControl = (AudioManager) context
            .getSystemService(Context.AUDIO_SERVICE);
    extras = getIntent().getExtras();
    if(extras!=null){
        startHour = extras.getInt("startHour");
        startMinute = extras.getInt("startMinute");
    }

    int minute = c.get(Calendar.MINUTE);
    int hour = c.get(Calendar.HOUR_OF_DAY);
    if(day.equals("Thursday")){
        if(hour==startHour){
            if(minute==startMinute){
                audioControl.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
            }
        }
    }

}

编辑:我的主要班级:

public class Settings extends Activity implements View.OnClickListener {


private PendingIntent pendingIntent;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.setting);
    Intent alarmIntent = new Intent(Settings.this, AlarmReceiver.class);
    alarmIntent.putExtra("day", "Tuesday");
    pendingIntent = PendingIntent.getBroadcast(Settings.this, 0, alarmIntent, 0);
    alarmRepeat();
}
  public void alarmRepeat(){
    Calendar cal = Calendar.getInstance();
    AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    manager.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 1000, pendingIntent);
}

4 个答案:

答案 0 :(得分:1)

只需检查你的onReceive()方法

@Override                                      
public void onReceive(Context context, Intent intent) {
                                              ^^^^^
    //you already have an instance of Intent object
    //just use it like this.
    Bundle extras=intent.getExtras();
}

答案 1 :(得分:0)

您不需要使用getIntent() - 方法。 Intent将在onReceive方法中作为参数提供。

而不是使用:

extras = getIntent().getExtras();

你可以简单地使用

extras = intent.getExtras();

答案 2 :(得分:0)

在您的活动中

Intent intent = new Intent("my.action.string");
intent.putExtra("startHour", ******); 
sendBroadcast(intent);

在你的班级AlarmReceiver中,更改

extras = getIntent().getExtras();

extras = intent.getExtras();
if(extras.equals("my.action.string")){
     String shour= intent.getExtras().getString("startHour");

}

和清单

<receiver android:name=".AlramReceiver" android:enabled="true">
    <intent-filter>
        <!-- and your existing actions  -->
        <action android:name="my.action.string" />
        <!-- and some more actions if you want -->
    </intent-filter>
</receiver>

答案 3 :(得分:0)

活动未实现可序列化 Parcelable 的接口,因此您无法将其作为Intent的参数传递,即你不能这样打电话:

intent.putExtra(string, Activity);

同时,当您注册BroadcastReceiver时,Intent已作为参数传递。同时,你可以得到它,这正是BroadcastReceiver的receive(Context,Intent)中的第二个参数。

@Override                                      
public void onReceive(Context context, Intent intent) {
    //intent is exactly the Intent you passed,into which you can put your startHour and startMinute integer values.
    int startHour = intent.getInt("startHour");
    int startMinute = intent.getInt("startMinute");
}