在我的应用程序中,我注意到这三件事:
- 从一个活动转到另一个活动时启用后退按钮,使用户能够单击回原始活动。问题是我不希望用户在我的应用程序中的某个点上单击“返回”。我不想在我的应用程序中完全禁用后退按钮,只有在调用一个intent时。我怎么能这样做?
- 我注意到一些奇怪的事情...当我的应用程序中弹出一个Toast通知时,一切都很好,直到我退出我的应用程序。当我退出我的应用程序时,一些Toast通知是残留的并且正在我的应用程序之外弹出。这有什么理由吗?我是否错过了活动生命周期中的某些内容来处理特定点的吐司取消?
最后,这个很难解决。如何锁定我的屏幕,以便当用户旋转设备时,活动不会再次被调用,并且asynctask仍然可以恢复而不重新开始?
非常感谢你的时间。只是好奇为什么会发生这些事情以及我应该注意什么?
这是我的代码:
//Main Activity.java
package com.example.Patient_Device;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import java.io.*;
public class MainActivity extends Activity {
//fields
private ProgressDialog progressBar;
private Context context;
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.start_setup);
//Set the context
context = this;
//Initialize the start setup button and add an onClick event listener to the button
final Button start_setup_button = (Button) findViewById(R.id.start_setup_button);
start_setup_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
//Executes the AsyncTask
new RetrieveInfoTask().execute();
//Instantiates the intent to launch a new activity
Intent myIntent = new Intent(MainActivity.this, RetrieveInfoActivity.class);
MainActivity.this.startActivity(myIntent);
}
});
}
public class RetrieveInfoTask extends AsyncTask<Void, Void, Void> {
//Called on the UI thread to execute progress bar
@Override
protected void onPreExecute() {
super.onPreExecute();
progressBar = new ProgressDialog(context);
progressBar.setIndeterminate(true);
progressBar.setCancelable(false);
progressBar.setMessage(MainActivity.this.getString(R.string.retrieve_info));
progressBar.show();
}
//Methods that retrieves information from the user device. This is performed in the Background thread
private void retrieveInfo() {
try {
//Reading the drawable resource line by line
String str="";
StringBuffer buf = new StringBuffer();
InputStream is = MainActivity.this.getResources().openRawResource(R.drawable.user_info);
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
if (is!=null) {
while ((str = reader.readLine()) != null) {
buf.append(str + "\n" );
}
}
is.close();
} catch (Exception e) {
e.printStackTrace();
}
}
//doInBackground calls retrieveInfo() to perform action in Background
@Override
protected Void doInBackground(Void... params) {
retrieveInfo();
return null;
}
//When the background task is done, dismiss the progress bar
@Override
protected void onPostExecute(Void result) {
if (progressBar!=null) {
progressBar.dismiss();
}
}
}
}
//RetrieveInfoActivity.java
package com.example.Patient_Device;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.Resources;
import android.os.BatteryManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;
public class RetrieveInfoActivity extends Activity {
private static String TAG = "RetrieveInfoActivity";
private Context context;
String fileLastSync = "09-18-2014 03:47 PM";
@Override
public void onCreate(Bundle savedInstanceState) {
context = this;
super.onCreate(savedInstanceState);
setContentView(R.layout.retrieve_info);
//Once the new activity is launched, the setup is complete
Toast.makeText(getApplicationContext(), "Setup Complete!",
Toast.LENGTH_LONG).show();
//Gets the 'last synced' string and sets to datetime of the last sync
Resources resources = context.getResources();
String syncString = String.format(resources.getString(R.string.last_sync), fileLastSync);
//Dynamically sets the datetime of the last sync string
TextView lastSyncTextView = ((TextView) findViewById(R.id.last_sync) );
lastSyncTextView.setText(syncString);
//calls registerReceiver to receive the broadcast for the state of battery
this.registerReceiver(this.mBatInfoReceiver,new
IntentFilter(Intent.ACTION_BATTERY_CHANGED));
}
private BroadcastReceiver mBatInfoReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context arg0, Intent intent) {
//Battery level
int level = intent.getIntExtra("level", 0);
//Dynamically sets the value of the battery level
TextView batteryTextView = ((TextView) findViewById(R.id.battery) );
batteryTextView.setText("Battery Level: " + String.valueOf(level)+ "%");
//If the battery level drops below 25%, then announce the battery is low
//TODO: Add 25 to constants file.
if(level < 25) {
Toast.makeText(getApplicationContext(), "Low Battery!",
Toast.LENGTH_LONG).show();
}
//Plugged in Status
int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
//Battery Status
int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
//If the device is charging or contains a full status, it's charging
boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING ||
status == BatteryManager.BATTERY_STATUS_FULL;
//If the device isCharging and plugged in, then show that the battery is charging
if(isCharging && plugged == BatteryManager.BATTERY_PLUGGED_AC || plugged == BatteryManager.BATTERY_PLUGGED_USB) {
Toast.makeText(getApplicationContext(), "Charging.." + String.valueOf(level)+ "%",
Toast.LENGTH_LONG).show();
}else{
Toast.makeText(getApplicationContext(), "Unplugged!",
Toast.LENGTH_LONG).show();
}
}
};
@Override
public void onDestroy() {
try {
super.onDestroy();
unregisterReceiver(this.mBatInfoReceiver);
}
catch (Exception e) {
Log.e(RetrieveInfoctivity.TAG, getClass() + " Releasing receivers-" + e.getMessage());
}
}
}
//StartSetupActivity.java
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class StartSetupActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
}
//FragmentsActivity.java
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class FragmentsActivity extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.main, container, false);
}
}
答案 0 :(得分:1)
首先,每当你想禁用后退时,只需覆盖onBackPressed()方法并删除super。像这样:
@Override
public void onBackPressed() {
//super.onBackPressed();
}
其次,您使用应用程序上下文来显示吐司。使用活动背景。
Toast.makeText(this or YourActivity.this, "Setup Complete!", Toast.LENGTH_LONG).show();
第三,只需将此属性添加到清单类中即可。这将避免在方向更改时重新调整您的活动
android:configChanges="orientation"
答案 1 :(得分:0)
我会按顺序回答这些问题:
后退按钮
您可以在Activity中覆盖onBackPressed并确定是要使用它还是让Android处理它。
@Override
public void onBackPressed()
{
// Set this how you want based on your app logic
boolean disallowBackPressed = false;
if (!disallowBackPressed)
{
super.onBackPressed();
}
}
<强>干杯强>
Toasts与通知管理器一起排队。如果您连续显示多个Toasts,它们会排队并一次显示一个,直到队列为空。
锁定活动方向
在清单中的活动元素上使用android:screenOrientation =“landscape”或android:screenOrientation =“portrait”来锁定方向。
答案 2 :(得分:0)
我认为应该单独询问这些问题,因为问题的每个项目的详细答案都太长了,但我希望这会有所帮助:
- 从一个活动转到另一个活动时启用后退按钮,使用户能够单击回原始活动。该 问题是我不希望用户在某一点点击Back 在我的申请中。我不想完全禁用后退按钮 在我的应用程序中,只有在调用一个intent时。我怎么能这样做?
您可以覆盖不希望用户返回的活动的onBackPressed。
@Override
public void onBackPressed() {
//Leave it blank so it doesn't do anything
}
- 我注意到一些奇怪的事情...当我的应用程序中弹出一个Toast通知时,一切都很好,直到我退出我的应用程序。当我退出我的 应用程序,一些Toast通知是残留的 弹出我的应用程序。这有什么理由吗?我有没有? 错过活动生命周期中的某些东西来处理取消 在某一点敬酒?
我认为其背后的原因是即使应用程序不再可见,烤面包也会进入,并按顺序显示。
最后,这个很难解决。如何锁定我的屏幕 当用户旋转设备时,活动不会 再次调用,asynctask仍然可以在不启动的情况下恢复 又一次?
为此,您可以在清单中使用以下代码
android:configChanges="orientation|screenSize"/>
但是谷歌不建议这样做,我建议您阅读以下链接以获取有关如何处理方向更改的更多信息: http://developer.android.com/guide/topics/resources/runtime-changes.html