在我的应用程序中,我注意到了以下三件事:
-当从一个活动转到另一个活动时,会启用后退按钮,使用户可以单击返回到原始活动。问题是我不希望用户在我的应用程序中的某个时刻单击"返回"。我不想在我的应用程序中完全禁用后退按钮,只有当调用一个意图时。我该怎么做?
-我注意到一些奇怪的事情。。。当我的应用程序中弹出toast通知时,一切都很好,直到我退出应用程序。当我退出应用程序时,一些toast通知是残留的,并且会弹出到应用程序之外。这有原因吗?在活动生命周期中,我是否错过了在某个时刻处理取消敬酒的事情?
最后,这个问题很难解决。如何锁定屏幕,以便在用户旋转设备时,不会再次调用活动,并且异步任务仍然可以在不重新启动的情况下继续?
非常感谢您抽出时间。只是好奇为什么会发生这些事情,我应该调查什么?
这是我的代码:
//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);
}
}
首先,每当您想要禁用backpress时,只需覆盖onBackPressed()方法并删除super。像这样:
@Override
public void onBackPressed() {
//super.onBackPressed();
}
其次,您正在使用应用程序上下文来显示toast。使用活动上下文。
Toast.makeText(this or YourActivity.this, "Setup Complete!", Toast.LENGTH_LONG).show();
第三,只需将此属性添加到清单类中。这将避免在方向改变时重复您的活动
android:configChanges="orientation"
我将按顺序回答这些问题:
后退按钮
你可以在你的活动中覆盖onBackPressed,并确定你是想消费它还是让Android处理它。
@Override
public void onBackPressed()
{
// Set this how you want based on your app logic
boolean disallowBackPressed = false;
if (!disallowBackPressed)
{
super.onBackPressed();
}
}
烤面包
Toast在通知管理器中排队。如果您连续显示多个Toast,它们会排队并一次显示一个,直到队列为空。
锁定活动方向
在清单中的活动元素上使用android:screenOrientation="landscape"或android:ScreenOrigination="portrait"来锁定方向。
我认为这些问题应该分开问,因为你的问题中每一项的详细答案都太长了,但我希望这会有所帮助:
-当从一个活动转到另一个活动时,会启用后退按钮,使用户可以单击返回到原始活动。这个问题是我不希望用户在某个时刻单击"返回"在我的申请中。我不想完全禁用后退按钮在我的应用程序中,仅当调用一个意向时。我该怎么做?
您可以在不希望用户返回的活动上覆盖onBackPressed。
@Override
public void onBackPressed() {
//Leave it blank so it doesn't do anything
}
-我注意到一些奇怪的事情。。。当我的应用程序中弹出toast通知时,一切都很好,直到我退出应用程序。当我退出我的应用程序,一些toast通知是剩余的跳出我的应用程序。这有原因吗?我有没有在活动生命周期中错过某些内容以处理的取消在某个时刻敬酒?
我认为这背后的原因是,即使应用程序不再可见,吐司也会变成一个que,并按顺序显示。
最后,这个问题很难解决。如何锁定屏幕当用户旋转设备时,活动不会再次被调用,异步任务仍然可以在不启动的情况下继续再来一遍?
为此,您可以在清单中使用以下代码
android:configChanges="orientation|screenSize"/>
然而,这不是谷歌推荐的,我建议你阅读以下链接,以获得更多关于如何处理方向变化的信息:http://developer.android.com/guide/topics/resources/runtime-changes.html