简体   繁体   English

从活动类使用方法

[英]Using Method from Activity Class

I am extending BroadcastReceiver class but in the given function I need to use getIntent() function which is a part of Activity class . 我正在扩展BroadcastReceiver类,但是在给定的函数中,我需要使用Activity类的一部分的getIntent()函数。 How should I use this function since I will only be able to extend just one Class. 我应该如何使用该功能,因为我只能扩展一个类。

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

}

EDIT : My main Class: 编辑 :我的主要班级:

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

Just check your onReceive() method 只需检查您的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();
}

You do not need to use getIntent()-method. 您不需要使用getIntent()方法。 The Intent will be provided as Parameter in your onReceive-method. Intent将作为onReceive方法中的参数提供。

Instead of using: 而不是使用:

extras = getIntent().getExtras();

you could simply use 你可以简单地使用

extras = intent.getExtras();

In your activity 在您的活动中

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

In your class AlarmReceiver, change 在您的类AlarmReceiver中,更改

extras = getIntent().getExtras();

to

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

}

and in menifest 并且明显

<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>

Activity doesn't implement interfaces of Serializable and Parcelable , so you can't pass it as a parameter of Intent, namely, you can't call like this: Activity没有实现SerializableParcelable的接口,因此您不能将其作为Intent的参数传递,即,您不能这样调用:

intent.putExtra(string, Activity);

Meanwhile, when you register a BroadcastReceiver, Intent has been passed as a paramter. 同时,当您注册BroadcastReceiver时,Intent已作为参数传递。 At the same time, you can get it which is exactly the second parameter in receive(Context, Intent) of BroadcastReceiver. 同时,您可以获得它,它恰好是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");
}

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

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