使用 AlarmManager 在片段中显示 toast

Show toast in fragment with AlarmManager

好的,我想使用 AlarmManager 在我的片段中显示 toast,但我不知道为什么我的 toast 不显示,这是我的代码:

我的Activity:

public class ScheduleMainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);    

        Configuration config = getResources().getConfiguration();

        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction =   fragmentManager.beginTransaction();
        ScheduleMainFragment fragment = new ScheduleMainFragment();

        if (config.orientation == Configuration.ORIENTATION_LANDSCAPE) {    
            fragmentTransaction.replace(android.R.id.content, fragment);
        }else{       
            fragmentTransaction.replace(android.R.id.content, fragment);
        }
        fragmentTransaction.commit();

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_login, menu);
        return true;
    }
}

这是我的片段:

public class ScheduleMainFragment extends Fragment {

    View _fragment;
    private PendingIntent pendingIntent;
    private AlarmManager manager;

    CircularProgressButton mScheduleDate;
    CircularProgressButton mMyScheduleDates;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {


        _fragment = inflater.inflate(R.layout.schedulemain_p, container, false);
        SetupView(_fragment);


        return _fragment;
    }

    private void SetupView(View fragment) {
        Intent alarmIntent = new Intent(getActivity().getApplicationContext(),AlarmReceiver.class);
        pendingIntent = PendingIntent.getBroadcast(getActivity(), 0, alarmIntent, 0);

        mScheduleDate = (CircularProgressButton) _fragment.findViewById(R.id.btScheduleDate);
        mMyScheduleDates = (CircularProgressButton) _fragment.findViewById(R.id.btMyScheduleDates);

        mScheduleDate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SetAlert((Button) view);
            }
        });
        mMyScheduleDates.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                StopAlert((Button) view);
            }
        });
    }


    private void SetAlert(Button view) {
        startAlarm();
    }

    private void StopAlert(Button view) {
    }


    public void startAlarm() {
        manager = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE);
        int interval = 10000;

        manager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), interval, pendingIntent);
        Toast.makeText(getActivity(), "Alarm Set", Toast.LENGTH_SHORT).show();
    }

我的AlarmReceiver

public class AlarmReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {

        Toast.makeText(context, "Alarma activada", Toast.LENGTH_SHORT);//When i debug this toast does not show in my fragment, i dont know what to do here.
    }
}

这里我的 AlarmReceiver 收到我的警报,但在我的片段中没有显示吐司,任何人都可以帮助我,我是 android 的新手。

您只是错过了对 show() 的呼叫。

public class AlarmReceiver extends BroadcastReceiver {

@Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, "Alarma activada", Toast.LENGTH_SHORT).show();
    }
}

您没有调用 show() 方法。

应该是:

Toast.makeText(context, "Alarma activada", Toast.LENGTH_SHORT).show();

试试这个,

显示吐司的基本语法:

Toast.makeText(context, text, duration);

context

getApplicationContext() - Returns the context for all activities running in application.

getBaseContext() - If you want to access Context from another context within application you can access.

getContext() - Returns the context view only current running activity.

text

text - Return "STRING" , If not string you can use type cast. (string)num // type caste

duration

Toast.LENGTH_SHORT - Toast delay 2000 ms predefined

Toast.LENGTH_LONG - Toast delay 3500 ms predefined

Toast Type

int LENGTH_LONG Show the view or text notification for a long period of time. int LENGTH_SHORT Show the view or text notification for a short period of time.

例如:

Toast.makeText(MainActivity.this, "Print Your Message", 5000).show();