我的活动:
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的新手。
答案 0 :(得分:2)
您没有调用show()
方法。
应该是:
Toast.makeText(context, "Alarma activada", Toast.LENGTH_SHORT).show();
答案 1 :(得分:2)
试试这个,
显示吐司的基本语法:
Toast.makeText(context, text, duration);
<强>上下文强>
getApplicationContext() - 返回所有活动的上下文 在应用程序中运行。
getBaseContext() - 如果要从其他上下文访问Context 在应用程序中,您可以访问。
getContext() - 仅返回当前运行活动的上下文视图。
文字强>
text - 返回“STRING”,如果不是字符串,则可以使用类型转换。 (字符串)num //输入caste
持续时间
Toast.LENGTH_SHORT - 吐司延迟2000毫秒预定义
Toast.LENGTH_LONG - Toast delay 3500 ms预定义
Toast Type
int LENGTH_LONG长时间显示视图或文本通知 时间int LENGTH_SHORT显示a的视图或文本通知 时间很短。
<强> E.g:强>
Toast.makeText(MainActivity.this, "Print Your Message", 5000).show();
答案 2 :(得分:1)
您在吐司上错过了show()
的来电。
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Alarma activada", Toast.LENGTH_SHORT).show();
}
}