我从onReceive()开始我的活动alarmAlert.class。 活动开始了。 我的活动上有三个按钮和两个TextView。 活动开始时,Textviews总会显示, 但按钮并不总是出现。
我从MainActivity开始活动,所有视图都有效 完美
我能做的一切。 谢谢
我得到了一些东西。 如果我的应用程序没有在后台运行(单击菜单按钮,从列表中删除应用程序) 活动从onReceive()开始,按钮不会显示。 如果应用程序仍在后台运行,一切都很完美。
我的onReceive代码:
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
int id = intent.getIntExtra("id", -1);
try{
Intent alertIntent=new Intent();
alertIntent.setClass(context, AlarmAlert.class);
Bundle bundle=new Bundle();
bundle.putInt("id", id);
alertIntent.putExtras(bundle);
alertIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_NO_USER_ACTION);
context.startActivity(alertIntent);
}
catch (Exception e){
AlarmGloble.writeLog(context,"AlarmReceiver:"+e.toString());
}
我的AlarmAlert代码的一部分
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
Log.d("AA","onCreate");
super.onCreate(savedInstanceState);
setContentView(R.layout.alarm_alert);
AlarmGloble.dbHelper=new DBHelper(this,AlarmGloble.DB_NAME,AlarmGloble.DB_VERSION);
AlarmGloble.dbLib=new DataBaseLib(AlarmGloble.dbHelper);
settingFromId();
findViews();
setListener();
updateView();
mVibrator=(Vibrator)getSystemService(Context.VIBRATOR_SERVICE);
setSystemVolume(0);
//play mp3 by Handler
mHandler.sendMessage(mHandler.obtainMessage(PLAY));
}
private void updateView(){
TextView textViewSnooze=(TextView)findViewById(R.id.alarm_alertTextViewSnooze);
if (mp3Test){
textViewSnooze.setVisibility(View.INVISIBLE);
spinnerSnooze.setVisibility(View.INVISIBLE);
buttonClose.setVisibility(View.INVISIBLE);
buttonEdit.setVisibility(View.INVISIBLE);
}
else {
textViewSnooze.setVisibility(View.VISIBLE);
spinnerSnooze.setVisibility(View.VISIBLE);
buttonClose.setVisibility(View.VISIBLE);
buttonEdit.setVisibility(View.VISIBLE);
if(alarmRept<=0){
textViewSnooze.setVisibility(View.VISIBLE);
spinnerSnooze.setVisibility(View.VISIBLE);
}
else{
textViewSnooze.setVisibility(View.INVISIBLE);
spinnerSnooze.setVisibility(View.INVISIBLE);
if (alarmReptCount<alarmReptTimes)
if (alarmKind==0)
mMessage=mMessage+"\n"+"next alarm :"+mClockSnooze[alarmRept]+" ";
else
mMessage=mMessage+"\n"+"next alarm:"+AlarmGloble.rept[alarmRept]+" ";
}
}
textViewMessage.setText(mMessage);
if(mMp3.equals(AlarmGloble.RANDOM)){
File file=new File(AlarmGloble.ROOT_MUSIC);
RandomMp3 rm=new RandomMp3(file);
if (rm.getCount()>0)
mMp3=rm.getFile();
else
mMp3="";
}
else {
if(mMp3.equals(AlarmGloble.DEFAULT)){
mMp3=getDefaultMP3();
}
else{
String[] s=mMp3.split("/");
textViewSongName.setText(s[s.length-1]);
}
}
if (mMp3.equals("")){
textViewSongName.setText(mMp3);
}
else {
String[] s=mMp3.split("/");
textViewSongName.setText(s[s.length-1]);
}
(LinearLayout)findViewById(R.id.alarm_alertLinearLayoutButtons);
linearLayoutButtons.requestFocus();
}
这里是alarm_alert.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/linearLayoutAlarmAlert" >
<LinearLayout
android:id="@+id/alarm_alertLinearLayoutButtons"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/alarm_alertButtonClose"
style="@style/Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btnClose" />
<Button
android:id="@+id/alarm_alertButtonEdit"
style="@style/Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btnEdit"
android:visibility="visible" />
<Button
android:id="@+id/alarm_alertButtonDismiss"
style="@style/Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btnExit"
android:visibility="visible" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/alarm_alertTextViewSnooze"
style="@style/RedLeft17sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/snooze" />
<Spinner
android:id="@+id/alarm_alertSpinnerSnooze"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
<TextView
android:id="@+id/alarm_alertTextViewMessage"
style="@style/YellowLeft17sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/btnClose" />
<TextView
android:id="@+id/alarm_alertTextViewSongName"
style="@style/YellowLeft17sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:text="@string/btnClose" />
</LinearLayout>
答案 0 :(得分:0)
我只能在AlarmAlert Activity中看到2个按钮buttonClose和buttonEdit,所以我假设没有显示的3个按钮位于alarm_alertLinearLayoutButtons.xml中,应该在alarm_alert.xml中重复使用
<include layout="@layout/alarm_alertLinearLayoutButtons"/>
参考:http://developer.android.com/training/improving-layouts/reusing-layouts.html
除了最后几行之外,我看不出有任何问题。 linearLayoutButtons未设置。
linearLayoutButtons = (LinearLayout)findViewById(R.id.alarm_alertLinearLayoutButtons);
linearLayoutButtons.requestFocus();