I'm developing an application which is using the android SpeechRecognizer. I'm using it for something simple. I click in a button, my SpeechRecognizer start listening and I got some results from what I said.
Easy right? Well, My problem is that I need to make SpeechRecognizer fast. I mean, I click in my button, I say "Hello" and SpeechRecognizer takes like 3-4 seconds in return an array with the possible results. My question is:
It's possible to make SpeechRecognizer return results more faster?
Or take less time to close the Listening intent and start to process what it listen?
Maybe another way to do it? which will have a better performance than this?
I was checking the library and I saw this 3 parameters:
EXTRA_SPEECH_INPUT_MINIMUM_LENGTH_MILLIS:
The amount of time that it should take after we stop hearing speech to consider the input complete.
EXTRA_SPEECH_INPUT_COMPLETE_SILENCE_LENGTH_MILLIS
The minimum length of an utterance.
EXTRA_SPEECH_INPUT_POSSIBLY_COMPLETE_SILENCE_LENGTH_MILLIS
The amount of time that it should take after we stop hearing speech to
consider the input possibly complete.
http://developer.android.com/intl/es/reference/android/speech/RecognizerIntent.html
I have tried all of them but it is not working, or maybe I'm not using them right. Here is my code:
public class MainActivity extends Activity {
private static final String TIME_FORMAT = "%02d:%02d:%02d";
private final String TAG = "MainActivity";
private StartTimerButton mSpeakButton;
private CircleProgressBar mCountdownProgressBar;
private CountDownTimer mCountDownTimer;
private TextView mTimer;
private int mRunSeconds = 0;
private SpeechRecognizer mSpeechRecognizer;
private Intent mSpeechRecognizerIntent;
private boolean mIsListening = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRunSeconds = 0;
mTimer = (TextView) findViewById(R.id.timerText);
mCountdownProgressBar = (CircleProgressBar) findViewById(R.id.progressBar);
mSpeechRecognizer = SpeechRecognizer.createSpeechRecognizer(this);
mSpeechRecognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,
this.getPackageName());
// mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_SPEECH_INPUT_MINIMUM_LENGTH_MILLIS,
// 1000);
// mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_SPEECH_INPUT_COMPLETE_SILENCE_LENGTH_MILLIS,
// 1000);
// mSpeechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_SPEECH_INPUT_POSSIBLY_COMPLETE_SILENCE_LENGTH_MILLIS,
// 1000);
SpeechRecognitionListener listener = new SpeechRecognitionListener();
mSpeechRecognizer.setRecognitionListener(listener);
mSpeakButton = (StartTimerButton) findViewById(R.id.btnSpeak);
mSpeakButton.setReadyState(false);
mSpeakButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mSpeakButton.isReady()) {
if (!mIsListening)
mSpeechRecognizer.startListening(mSpeechRecognizerIntent);
} else
mSpeakButton.setReadyState(true);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
return true;
}
public void onSpeechResults(ArrayList<String> matches) {
for (String match : matches) {
match = match.toLowerCase();
Log.d(TAG, "Got speech: " + match);
if (match.contains("go")) {
//Do Something
mSpeechRecognizer.stopListening();
}
if (match.contains("stop")) {
//Do Something
mSpeechRecognizer.stopListening();
}
}
}
protected class SpeechRecognitionListener implements RecognitionListener
{
#Override
public void onBeginningOfSpeech()
{
//Log.d(TAG, "onBeginingOfSpeech");
}
#Override
public void onBufferReceived(byte[] buffer)
{
}
#Override
public void onEndOfSpeech()
{
//Log.d(TAG, "onEndOfSpeech");
}
#Override
public void onError(int error)
{
mSpeechRecognizer.startListening(mSpeechRecognizerIntent);
//Log.d(TAG, "error = " + error);
}
#Override
public void onEvent(int eventType, Bundle params)
{
}
#Override
public void onPartialResults(Bundle partialResults)
{
ArrayList<String> matches = partialResults.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
for (String match : matches) {
match = match.toLowerCase();
Log.d(TAG, "onPartialResults : " + match);
}
}
#Override
public void onReadyForSpeech(Bundle params)
{
Log.d(TAG, "onReadyForSpeech"); //$NON-NLS-1$
}
#Override
public void onResults(Bundle results)
{
//Log.d(TAG, "onResults"); //$NON-NLS-1$
ArrayList<String> matches = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
onSpeechResults(matches);
// matches are the return values of speech recognition engine
// Use these values for whatever you wish to do
}
#Override
public void onRmsChanged(float rmsdB)
{
}
}}
Yes, it is possible to reduce the delay before shutdown....
You cannot alter the amount of time that Google considers to be silence at the end of a user speaking. The EXTRA_SPEECH_* parameters used to work, now they appear to sporadically work at best, or not work at all.
What you can do, is use the partial results to detect the words or phrase you want and then manually shut down the recognition service.
Here's an example of how to do this:
public boolean isHelloDetected(#NonNull final Context ctx, #NonNull final Locale loc, #NonNull final Bundle results) {
boolean helloDetected = false;
if (!results.isEmpty()) {
final String hello = ctx.getString(R.string.hello);
final ArrayList<String> partialData = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
/* handles empty string bug */
if (partialData != null && !partialData.isEmpty()) {
partialData.removeAll(Collections.singleton(""));
if (!partialData.isEmpty()) {
final ListIterator<String> itr = partialData.listIterator();
String vd;
while (itr.hasNext()) {
vd = itr.next().toLowerCase(loc).trim();
if (vd.startsWith(hello)) {
helloDetected = true;
break;
}
}
}
}
if (!helloDetected) {
final ArrayList<String> unstableData = results.getStringArrayList("android.speech.extra.UNSTABLE_TEXT");
/* handles empty string bug */
if (unstableData != null && !unstableData.isEmpty()) {
unstableData.removeAll(Collections.singleton(""));
if (!unstableData.isEmpty()) {
final ListIterator<String> itr = unstableData.listIterator();
String vd;
while (itr.hasNext()) {
vd = itr.next().toLowerCase(loc).trim();
if (vd.startsWith(hello)) {
helloDetected = true;
break;
}
}
}
}
}
}
return helloDetected;
}
You would run this method each time you receive from onPartialResults()
If true is returned, you'll need to call stopListening() on the main thread (probably by new Handler(Looper.getMainLooper()).post(...
Be aware though, once you've shut down the recognizer, the subsequent and final results you receive in onResults() may not contain "hello". As that word may have only be classified as unstable.
You'll need to write additional logic to prevent using detectHello() once hello has been detected (otherwise you'll repeatedly call stopListening()) - some simple boolean markers would resolve this.
Finally, the use of Collections.singleton("") to remove empty strings is part of an internal bug report, details to replicate here and the use of a ListIterator may be overkill for just your sample; a simple for loop would suffice.
Good luck.
Related
Has anyone tried this API?
I'm developing a privileged system app to scan the network.
For some reason I get error 2 'Wrong parameters' when I try using it.
Granted the app MODIFY_PHONE_STATE permission, and still get the error
Here's an extract:
public class ScannerActivity extends Activity implements View.OnClickListener {
private final int PHONE_STATE_REQUEST = 1;
private Button scanButton;
private TextView resultsTextView;
private class RadioCallback extends TelephonyScanManager.NetworkScanCallback {
private List<CellInfo> mCellInfoResults;
private int mScanError;
#Override
public void onResults(List<CellInfo> cellInfoResults) {
mCellInfoResults = cellInfoResults;
ScannerActivity.this.runOnUiThread(new Runnable() {
#Override
public void run() {
for (CellInfo cellInfo:mCellInfoResults) {
resultsTextView.append(" " + cellInfo.toString() + " ");
}
}
});
}
#Override
public void onError(int error) {
mScanError = error;
ScannerActivity.this.runOnUiThread(new Runnable() {
#Override
public void run() {
resultsTextView.append(" Error: " + mScanError);
}
});
}
#Override
public void onComplete() {
ScannerActivity.this.runOnUiThread(new Runnable() {
#Override
public void run() {
resultsTextView.append(" Scan Completed! ");
}
});
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scanner);
scanButton = (Button) findViewById(R.id.scan_button);
scanButton.setOnClickListener(this);
resultsTextView = (TextView) findViewById(R.id.results_text_view);
}
public void onClick(View view) {
NetworkScanRequest networkScanRequest;
RadioAccessSpecifier radioAccessSpecifiers[];
int bands[];
ArrayList<String> PLMNIds = new ArrayList<String>(Arrays.asList("42501"));
TelephonyManager telephonyManager = (TelephonyManager) getApplicationContext()
.getSystemService(Context.TELEPHONY_SERVICE);
bands[0] = AccessNetworkConstants.UtranBand.BAND_1;
radioAccessSpecifiers = new RadioAccessSpecifier[1];
radioAccessSpecifiers[0] = new RadioAccessSpecifier(
AccessNetworkConstants.AccessNetworkType.UTRAN,
bands,
null);
networkScanRequest = new NetworkScanRequest(
NetworkScanRequest.SCAN_TYPE_ONE_SHOT,
radioAccessSpecifiers,
0,
60,
false,
0,
PLMNIds);
telephonyManager.requestNetworkScan(networkScanRequest, AsyncTask.SERIAL_EXECUTOR,new RadioCallback());
}
}
Does anyone has an idea what parameters did i pass wrong?
I first thought that it might be LTE / 3G issue, but I get the same results both for UTRAN & EUTRAN.
I also ruled out SIM issue - the same error happens with 2 different SIMS, both successfully scanning the network form settings app for example.
OK, figured it out - for anyone who may come across this issue -
searchPeriodicity & incrementalResultsPeriodicity cannot be 0, despite the documentation,
and irrelevance of these parameters for one shot scan.
I'm trying to make a timeline fragment for my app that shows the number of steps taken each day, I display the step count in a fragment,
I'm having trouble in resetting the value after every 24 hours.
I've declared my step counting variable as a static variable which resets when app is closed but I have declared a service class which is where my step counting logic happens.
Each day's steps will be inserted into a sqlite database and displayed in another fragment inside a listview.
public class HomeFragment extends Fragment {
TextView tv_steps;
View myView;
private BroadcastReceiver receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
int stepCountValue = intent.getExtras().getInt("StepCount");
tv_steps.setText(String.valueOf(stepCountValue));
// Toast.makeText(context, stepCountValue + "", Toast.LENGTH_SHORT).show();
}
};
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
myView = inflater.inflate(R.layout.home_layout, container, false);
RelativeLayout rl = (RelativeLayout) inflater.inflate(R.layout.home_layout, container, false);
tv_steps = (TextView) rl.findViewById(R.id.tv_steps);
return rl;
}
#Override
public void onResume() {
super.onResume();
getActivity().registerReceiver(receiver, new IntentFilter("stepcountvalue"));
}
#Override
public void onPause() {
super.onPause();
//getActivity().unregisterReceiver(receiver);
}
}
My service class for step counting logic:
public class StepCounterService extends Service implements SensorEventListener {
SensorManager sensorManager;
static int initialStepCount = 0;
public static int stepCount = 0;
SQLiteDatabase database;
private int numberOfStepsToBeInserted;
#Override
public void onCreate() {
super.onCreate();
sensorManager = (SensorManager) this.getSystemService(Context.SENSOR_SERVICE);
database = openOrCreateDatabase("Steps", MODE_PRIVATE, null);
try {
database.execSQL("CREATE TABLE IF NOT EXISTS steps (date TEXT, steps_count INT(10))");
} catch (Exception e) {
e.printStackTrace();
}
final Handler handler = new Handler();
Runnable run = new Runnable() {
#Override
public void run() {
//numberOfStepsToBeInserted = stepCount;
//stepCount = stepCount - numberOfStepsToBeInserted;
//stepCount = 0;
String date = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
database.execSQL("INSERT INTO steps(date, steps_count) VALUES('" + date + "', '" + stepCount + "')");
Cursor c = database.rawQuery("SELECT * FROM steps", null);
int dateIndex = c.getColumnIndex("date");
int stepIndex = c.getColumnIndex("steps_count");
c.moveToFirst();
if (c.getCount() >= 1) {
while (c.moveToNext()) {
Log.d("----------------->", c.getString(dateIndex) + " " + c.getInt(stepIndex));
}
} else {
Log.i("Check empty", "Cursor empty");
}
handler.postDelayed(this, 86400000);
}
};
handler.post(run);
}
#Override
public void onSensorChanged(SensorEvent event) {
if (initialStepCount == 0) {
initialStepCount = (int) event.values[0];
}
stepCount = (int) event.values[0] - initialStepCount;
Intent sendStepCount = new Intent("stepcountvalue");
sendStepCount.putExtra("StepCount", stepCount - numberOfStepsToBeInserted);
sendBroadcast(sendStepCount);
}
#Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
Sensor countSensor = sensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER);
if (countSensor != null) {
sensorManager.registerListener(this, countSensor, SensorManager.SENSOR_DELAY_UI);
} else {
}
return START_STICKY;
}
#Override
public void onDestroy() {
}
}
It might be easier for you to accomplish this using the Google Fit API. You'll have to look through it a bit to figure out exactly which part of it you need to use, but it allows much more functionality and will be a lot better for performance than running a background service the entire time. Also, with the addition of Doze and App Standby in Android 6.0+, your service might be stopped when the screen is turned off, making the resulting data inaccurate and unreliable.
If you do not wish to use the Google Fit API, then I would recommend two things to fix your problems:
Use [AlarmManager.set()](https://developer.android.com/reference/android/app/AlarmManager.html#set(int, long, android.app.PendingIntent)) along with [PendingIntent.getBroadcast()](https://developer.android.com/reference/android/app/PendingIntent.html#getBroadcast(android.content.Context, int, android.content.Intent, int)) to reset the value. You will need to schedule a new alarm for 24 hours later every time the BroadcastReciever is used.
Static variables are usually unpredictable and difficult to track or debug. It might be better to create a class that extends Application and setting it as your app's android:name attribute in your AndroidManifest.xml (as per this other guy's answer), storing the value there, then accessing it like so: ((MyApplication) getContext().getApplicationContext()).thisIsAValueName;.
As for the third problem (didn't really look like a problem, but I'm assuming it is one since it was listed with two other problems before it), the question is a little too generic to answer without going into a lot of detail:
Each day's steps will be inserted into a sqlite database and displayed in another fragment inside a listview.
I would recommend finding a tutorial of some sort to help you with this instead of asking it on Stack Overflow, such as this one, for example.
I have developed an app capable of detecting BLE signals and others parameters. I use a BaseAdapter to develop the ListView for showing each item. The problem is that I want to save those data in a xml file when the scan has finished (after a period of time I have established) but I donĀ“t know how to do it.
In this class I do the scan of BLE and is where I want to initiate the process of saving the List when it has passed the time of scanning:
public class ScanBleActivity extends ScanBaseActivity {
private BluetoothAdapter mBluetoothAdapter;
private boolean mScanning;
private Handler mHandler = new Handler();
//private List<BluetoothDevice> mydata;
// Stops scanning after 10 seconds.
private static final long SCAN_PERIOD = 20000;
/* (non-Javadoc)
* #see com.zishao.bletest.ScanBaseActivity#initScanBluetooth()
*/
protected void initScanBluetooth() {
BluetoothManager manager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
mBluetoothAdapter = manager.getAdapter();
startScanLen(true);
}
#Override
protected void onDestroy() {
super.onDestroy();
if (mScanning) {
startScanLen(false);
}
}
/**
*
* #param enable
*/
private void startScanLen(final boolean enable) {
if (enable) {
// Stops scanning after a pre-defined scan period.
mHandler.postDelayed(new Runnable() {
#Override
public void run() {
mScanning = false;
mBluetoothAdapter.stopLeScan(mLeScanCallback);
try {
savedata(true);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}, SCAN_PERIOD);
mScanning = true;
mBluetoothAdapter.startLeScan(mLeScanCallback);
} else {
mScanning = false;
mBluetoothAdapter.stopLeScan(mLeScanCallback);
}
}
And here is my Adapter:
public class LeDeviceListAdapter extends BaseAdapter {
public List<BluetoothDevice> data;
private Activity context;
private final HashMap<BluetoothDevice, Integer> rssiMap = new HashMap<BluetoothDevice, Integer>();
public LeDeviceListAdapter(Activity context, List<BluetoothDevice> data) {
this.data = data;
this.context = context;
}
//public static List<BluetoothDevice> getAllData() {
// return data;
//}
public synchronized void addDevice(BluetoothDevice device, int rssi) {
if(!data.contains(device) ){
data.add(device);
}
rssiMap.put(device, rssi);
}
#Override
public int getCount() {
return data.size();
}
#Override
public Object getItem(int position) {
return data.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (null == convertView) {
LayoutInflater mInflater =
(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.leaf_devices_list_item, null);
convertView.setTag(new DeviceView(convertView));
}
DeviceView view = (DeviceView) convertView.getTag();
view.init((BluetoothDevice) getItem(position));
return convertView;
}
public class DeviceView {
private TextView title;
private TextView status;
private TextView type;
private TextView address;
private TextView rssivalue;
public DeviceView(View view) {
title = (TextView) view.findViewById(R.id.device_name);
status = (TextView) view.findViewById(R.id.device_status_txt);
type = (TextView) view.findViewById(R.id.device_type_txt);
address = (TextView) view.findViewById(R.id.device_address_txt);
rssivalue = (TextView) view.findViewById(id.signal_intensity_txt);
}
public void init(BluetoothDevice device) {
title.setText(device.getName());
address.setText(device.getAddress());
setType(device.getType());
setStatus(device.getBondState());
rssivalue.setText(""+rssiMap.get(device)+" dBm");
}
public void setType(int status) {
switch(status) {
case BluetoothDevice.DEVICE_TYPE_CLASSIC:
type.setText("Bluetooth Signal");
break;
case BluetoothDevice.DEVICE_TYPE_LE:
type.setText("BLE Signal");
break;
case BluetoothDevice.DEVICE_TYPE_DUAL:
type.setText("Dual Mode - BR/EDR/LE");
break;
case BluetoothDevice.DEVICE_TYPE_UNKNOWN:
type.setText("Device Unknown");
break;
}
}
public void setStatus(int s) {
switch(s) {
case BluetoothDevice.BOND_NONE:
status.setText("Not Bonded");
break;
case BluetoothDevice.BOND_BONDED:
status.setText("Bonded");
break;
case BluetoothDevice.BOND_BONDING:
status.setText("Bonding");
break;
}
}
}
I want to save the title, address, type, status and rssivalue (shown in the code above) of each BLE signal that has been found during the scan to be saved in a xml file. I have provided only a part of the project but if it is necessarly I will edit and put the code that is missing.
Does anyone know how to do it?? Please help!!!!!
New code: This corresponds to the class ScanBaseActivity:
abstract public class ScanBaseActivity extends ListActivity {
protected LeDeviceListAdapter mLeDeviceListAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_devices_scan);
mLeDeviceListAdapter = new LeDeviceListAdapter(this, new ArrayList<BluetoothDevice>());
this.setListAdapter(mLeDeviceListAdapter);
initScanBluetooth();
}
/**
* Start Scan Bluetooth
*
*/
abstract protected void initScanBluetooth();
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
BluetoothDevice device = (BluetoothDevice) mLeDeviceListAdapter.getItem(position);
ParcelUuid[] uuids = device.getUuids();
String uuidString = "Getting UUID's from " + device.getName() + ";UUID:";
if (null != uuids && uuids.length > 0) {
uuidString += uuids[0].getUuid().toString();
} else {
uuidString += "empty";
}
Toast.makeText(this, uuidString, Toast.LENGTH_LONG).show();
}
/**
* #param device
*/
protected synchronized void addDevice(final BluetoothDevice device, final int rssi) {
runOnUiThread(new Runnable() {
#Override
public void run() {
mLeDeviceListAdapter.addDevice(device, rssi);
mLeDeviceListAdapter.notifyDataSetChanged();
}
});
}
protected void savedata(boolean enable) throws FileNotFoundException{
String filename = "file.txt";
FileOutputStream fos;
Bundle extras = getIntent().getExtras();
long timestamp = extras.getLong("currentTime");
try {
fos= openFileOutput(filename, Context.MODE_PRIVATE);
ObjectOutputStream out = new ObjectOutputStream(fos);
out.write((int) timestamp);
out.writeObject(mLeDeviceListAdapter);
out.write(null);
out.close();
Toast.makeText(this, R.string.list_saved, Toast.LENGTH_SHORT).show();
savedata(false);
} catch (FileNotFoundException e){
e.printStackTrace();
} catch (IOException e){
e.printStackTrace();
}
}
}
New!!: I have edited ScanBaseActivity and ScanBleActivity to introduce the xml saving but when I run the app, when the scan stops it causes an error (moment when the list has to be saved in the sml file). Does anyone know how to solve it or correct it??!!!
OK, first, you need to re-shape how you are dealing with your adapter, after that, everything should fall into place.
so for that, I'm going to outsource to vogella, the cardinal of good android design patterns
http://www.vogella.com/tutorials/AndroidListView/article.html
you could read up through section 3, eat some copypasta and come back here, but every extra line you grok is a good thing =]
Now you have an activity that has a list of data, and an adapter that takes that list and--somewhat dumbly in comparison to your code--applies it to a view of some sort. When you want to update that data, you do so by modifying the List object in the activity by your method somewhere that gets a list of Bluetoothdevices--and I'd take that process off thread with an AsyncTask.
Since you are passing a List to the adapter, you can wait until you've populated the data in your activity, then do an adapter.notifyDataSetChanged. You do not want to adapter.add
then you have a nice List of your data in the activity; you don't need to worry if it's the right list, because--given the update pattern--it's the only list!
then follow the link Merleverde posted to serialize that data into an xml, maybe in a utility class, but in your activity is fine.
edit:
here is a perfectly good adapter, this is part of the larger pattern that displays dynamically changing data:
public class AnchorListAdapter extends ArrayAdapter<String>
{
private final List<String> anchorNames;
public AnchorListAdapter(Context context, int textviewId, List<String> anchors){
super(context, textviewId, anchors);
anchorNames = anchors;
}
#Override
public String getItem( int i ) {
return anchorNames.get(i).toString();
}
}
Well, it's not that you are going to be saving it from the adapter, it's that the adater is going to be 'adapting' a data set that you will put into preferences.
as a feild:
private SharedPreferences saveHash;
in onCreate:
saveHash = getSharedPreferences( getString( R.string.save_hash ), MODE_PRIVATE );
then later:
public void onFinishedLoading(){
super.onPause();
SharedPreferences.Editor editor = saveHash.edit();
editor.clear();
for( String s: myData){
editor.putString(x, s);
}
editor.commit();
}
edit: realized you want to make a hash from a list; what do you want as the key?
I'm trying to use an AsyncTaskLoader to load data in the background to populate a detail view in response to a list item being chosen. I've gotten it mostly working but I'm still having one issue. If I choose a second item in the list and then rotate the device before the load for the first selected item has completed, then the onLoadFinished() call is reporting to the activity being stopped rather than the new activity. This works fine when choosing just a single item and then rotating.
Here is the code I'm using. Activity:
public final class DemoActivity extends Activity
implements NumberListFragment.RowTappedListener,
LoaderManager.LoaderCallbacks<String> {
private static final AtomicInteger activityCounter = new AtomicInteger(0);
private int myActivityId;
private ResultFragment resultFragment;
private Integer selectedNumber;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
myActivityId = activityCounter.incrementAndGet();
Log.d("DemoActivity", "onCreate for " + myActivityId);
setContentView(R.layout.demo);
resultFragment = (ResultFragment) getFragmentManager().findFragmentById(R.id.result_fragment);
getLoaderManager().initLoader(0, null, this);
}
#Override
protected void onDestroy() {
super.onDestroy();
Log.d("DemoActivity", "onDestroy for " + myActivityId);
}
#Override
public void onRowTapped(Integer number) {
selectedNumber = number;
resultFragment.setResultText("Fetching details for item " + number + "...");
getLoaderManager().restartLoader(0, null, this);
}
#Override
public Loader<String> onCreateLoader(int id, Bundle args) {
return new ResultLoader(this, selectedNumber);
}
#Override
public void onLoadFinished(Loader<String> loader, String data) {
Log.d("DemoActivity", "onLoadFinished reporting to activity " + myActivityId);
resultFragment.setResultText(data);
}
#Override
public void onLoaderReset(Loader<String> loader) {
}
static final class ResultLoader extends AsyncTaskLoader<String> {
private static final Random random = new Random();
private final Integer number;
private String result;
ResultLoader(Context context, Integer number) {
super(context);
this.number = number;
}
#Override
public String loadInBackground() {
// Simulate expensive Web call
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return "Item " + number + " - Price: $" + random.nextInt(500) + ".00, Number in stock: " + random.nextInt(10000);
}
#Override
public void deliverResult(String data) {
if (isReset()) {
// An async query came in while the loader is stopped
return;
}
result = data;
if (isStarted()) {
super.deliverResult(data);
}
}
#Override
protected void onStartLoading() {
if (result != null) {
deliverResult(result);
}
// Only do a load if we have a source to load from
if (number != null) {
forceLoad();
}
}
#Override
protected void onStopLoading() {
// Attempt to cancel the current load task if possible.
cancelLoad();
}
#Override
protected void onReset() {
super.onReset();
// Ensure the loader is stopped
onStopLoading();
result = null;
}
}
}
List fragment:
public final class NumberListFragment extends ListFragment {
interface RowTappedListener {
void onRowTapped(Integer number);
}
private RowTappedListener rowTappedListener;
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
rowTappedListener = (RowTappedListener) activity;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
ArrayAdapter<Integer> adapter = new ArrayAdapter<Integer>(getActivity(),
R.layout.simple_list_item_1,
Arrays.asList(1, 2, 3, 4, 5, 6));
setListAdapter(adapter);
}
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
ArrayAdapter<Integer> adapter = (ArrayAdapter<Integer>) getListAdapter();
rowTappedListener.onRowTapped(adapter.getItem(position));
}
}
Result fragment:
public final class ResultFragment extends Fragment {
private TextView resultLabel;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.result_fragment, container, false);
resultLabel = (TextView) root.findViewById(R.id.result_label);
if (savedInstanceState != null) {
resultLabel.setText(savedInstanceState.getString("labelText", ""));
}
return root;
}
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString("labelText", resultLabel.getText().toString());
}
void setResultText(String resultText) {
resultLabel.setText(resultText);
}
}
I've been able to get this working using plain AsyncTasks but I'm trying to learn more about Loaders since they handle the configuration changes automatically.
EDIT: I think I may have tracked down the issue by looking at the source for LoaderManager. When initLoader is called after the configuration change, the LoaderInfo object has its mCallbacks field updated with the new activity as the implementation of LoaderCallbacks, as I would expect.
public <D> Loader<D> initLoader(int id, Bundle args, LoaderManager.LoaderCallbacks<D> callback) {
if (mCreatingLoader) {
throw new IllegalStateException("Called while creating a loader");
}
LoaderInfo info = mLoaders.get(id);
if (DEBUG) Log.v(TAG, "initLoader in " + this + ": args=" + args);
if (info == null) {
// Loader doesn't already exist; create.
info = createAndInstallLoader(id, args, (LoaderManager.LoaderCallbacks<Object>)callback);
if (DEBUG) Log.v(TAG, " Created new loader " + info);
} else {
if (DEBUG) Log.v(TAG, " Re-using existing loader " + info);
info.mCallbacks = (LoaderManager.LoaderCallbacks<Object>)callback;
}
if (info.mHaveData && mStarted) {
// If the loader has already generated its data, report it now.
info.callOnLoadFinished(info.mLoader, info.mData);
}
return (Loader<D>)info.mLoader;
}
However, when there is a pending loader, the main LoaderInfo object also has an mPendingLoader field with a reference to a LoaderCallbacks as well, and this object is never updated with the new activity in the mCallbacks field. I would expect to see the code look like this instead:
// This line was already there
info.mCallbacks = (LoaderManager.LoaderCallbacks<Object>)callback;
// This line is not currently there
info.mPendingLoader.mCallbacks = (LoaderManager.LoaderCallbacks<Object>)callback;
It appears to be because of this that the pending loader calls onLoadFinished on the old activity instance. If I breakpoint in this method and make the call that I feel is missing using the debugger, everything works as I expect.
The new question is: Have I found a bug, or is this the expected behavior?
In most cases you should just ignore such reports if Activity is already destroyed.
public void onLoadFinished(Loader<String> loader, String data) {
Log.d("DemoActivity", "onLoadFinished reporting to activity " + myActivityId);
if (isDestroyed()) {
Log.i("DemoActivity", "Activity already destroyed, report ignored: " + data);
return;
}
resultFragment.setResultText(data);
}
Also you should insert checking isDestroyed() in any inner classes. Runnable - is the most used case.
For example:
// UI thread
final Handler handler = new Handler();
Executor someExecutorService = ... ;
someExecutorService.execute(new Runnable() {
public void run() {
// some heavy operations
...
// notification to UI thread
handler.post(new Runnable() {
// this runnable can link to 'dead' activity or any outer instance
if (isDestroyed()) {
return;
}
// we are alive
onSomeHeavyOperationFinished();
});
}
});
But in such cases the best way is to avoid passing strong reference on Activity to another thread (AsynkTask, Loader, Executor, etc).
The most reliable solution is here:
// BackgroundExecutor.java
public class BackgroundExecutor {
private static final Executor instance = Executors.newSingleThreadExecutor();
public static void execute(Runnable command) {
instance.execute(command);
}
}
// MyActivity.java
public class MyActivity extends Activity {
// Some callback method from any button you want
public void onSomeButtonClicked() {
// Show toast or progress bar if needed
// Start your heavy operation
BackgroundExecutor.execute(new SomeHeavyOperation(this));
}
public void onSomeHeavyOperationFinished() {
if (isDestroyed()) {
return;
}
// Hide progress bar, update UI
}
}
// SomeHeavyOperation.java
public class SomeHeavyOperation implements Runnable {
private final WeakReference<MyActivity> ref;
public SomeHeavyOperation(MyActivity owner) {
// Unlike inner class we do not store strong reference to Activity here
this.ref = new WeakReference<MyActivity>(owner);
}
public void run() {
// Perform your heavy operation
// ...
// Done!
// It's time to notify Activity
final MyActivity owner = ref.get();
// Already died reference
if (owner == null) return;
// Perform notification in UI thread
owner.runOnUiThread(new Runnable() {
public void run() {
owner.onSomeHeavyOperationFinished();
}
});
}
}
Maybe not best solution but ...
This code restart loader every time, which is bad but only work around that works - if you want to used loader.
Loader l = getLoaderManager().getLoader(MY_LOADER);
if (l != null) {
getLoaderManager().restartLoader(MY_LOADER, null, this);
} else {
getLoaderManager().initLoader(MY_LOADER, null, this);
}
BTW. I am using Cursorloader ...
A possible solution is to start the AsyncTask in a custom singleton object and access the onFinished() result from the singleton within your Activity. Every time you rotate your screen, go onPause() or onResume(), the latest result will be used/accessed. If you still don't have a result in your singleton object, you know it is still busy or that you can relaunch the task.
Another approach is to work with a service bus like Otto, or to work with a Service.
Ok I'm trying to understand this excuse me if I misunderstood anything, but you are losing references to something when the device rotates.
Taking a stab...
would adding
android:configChanges="orientation|keyboardHidden|screenSize"
in your manifest for that activity fix your error? or prevent onLoadFinished() from saying the activity stopped?
This question is about using the Google Android SDK, in the Java programming language.
My question could be boiled down to: Why is this code causing the android emulator to crash?
I've been wrestling for a few days with concurrency related to setting up different threads for a game app.
I have made many variations, but they have all failed. At this point, I just want to get a basic concurrent setup going. The worst part is that it is the emulator that crashes, so DDMS reports nothing; therefore I'm pretty clueless as to where the issue is.
The following code shows an activity (class Main), that calls class SceneManager, which creates a thread to be used for game logic stuff. A 3rd class, StatusChannel, is (will be) used to communicate status information between the different threads (Eventually, there will also be a OpenGL rendering thread).
The emulator crashes at different times. It may run for 20 seconds or for 5 minutes.
The setContentView(R.layout.main) in the Activity class just the set basic layout that Eclipse creates.
I've commented out the usage of Node (Created in the Activity class and accessed in SceneManager)
I have installed sdk versions 1.5 through 2.3 -- The current app is targeted at 2.1
The issue has something to do with the SceneManager class. I'm specially suspicious of the run() method.
Here are the 3 classes.
Sorry for the code length.
public class Main extends Activity {
private SceneManager mSceneManager;
private volatile Node mSceneGraph = new Node();
private volatile Status mStatusChannel = new Status();
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
Log.d("-- Main", "onCreate()");
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Holds the scene assets, such as the stage,
// the agents, camera, etc.
mSceneManager = new SceneManager(mSceneGraph, mStatusChannel);
mSceneManager.onCreate();
}
#Override
protected void onResume() {
Log.d("-- Main", "onResume()");
super.onResume();
mSceneManager.onResume();
}
#Override
protected void onPause() {
Log.d("-- Main", "onPause()");
super.onPause();
mSceneManager.onPause();
}
#Override
protected void onDestroy() {
Log.d("-- Main", "onDestroy()");
super.onDestroy();
mSceneManager.onDestroy();
}
}
public class SceneManager implements Runnable{
private Thread mThread;
private volatile Status mStatusChannel;
private volatile Node mSceneGraph;
private volatile long mMillis = 0;
private volatile PrepareVisitor mPrepareVisitor;
private volatile int mStatus = Status.UNKNOWN_STATUS;
SceneManager(Node sceneGraph, Status statusChannel) {
mPrepareVisitor = new PrepareVisitor();
mStatusChannel = statusChannel;
mSceneGraph = sceneGraph;
mMillis = SystemClock.uptimeMillis();
mThread = new Thread(this);
mThread.setName("LogicThread");
mStatusChannel.setSceneManagerStatus(Status.READY_STATUS);
}
public void onCreate() {
Log.d("-- SceneManager", "onCreate()...");
// This will start the thread in a paused state.
mThread.start();
}
public void onResume() {
Log.d("-- SceneManager", "onResume()...");
// Unpause the status manager, if it is currently paused.
if (mStatusChannel.getSceneManagerStatus() == Status.PAUSED_STATUS) {
mStatusChannel.setSceneManagerStatus(Status.READY_STATUS);
}
}
public void onPause() {
Log.d("-- SceneManager", "onPause()...");
if (mStatusChannel.getSceneManagerStatus() != Status.UNKNOWN_STATUS) {
mStatusChannel.setSceneManagerStatus(Status.PAUSED_STATUS);
}
}
public void onDestroy() {
mStatusChannel.setSceneManagerStatus(Status.QUIT_STATUS);
try {
mThread.join();
}
catch (InterruptedException e) {
Log.d("-- SceneManager", "InterruptedException");
}
}
/**
* This method should not be called by clients of this class.
*/
#Override
public void run() {
Log.d("-- SceneManager", "Called...");
// Main logic loop.
outer: while (true) {
// How much time has elapsed since last call.
long timeDelta = SystemClock.uptimeMillis() - mMillis;
switch (mStatus) {
case Status.READY_STATUS:
//mPrepareVisitor.go(mSceneGraph, timeDelta);
break;
case Status.PAUSED_STATUS:
break;
case Status.QUIT_STATUS:
break outer;
case Status.UNKNOWN_STATUS:
int renderStatus = mStatusChannel.getRendererStatus();
if (renderStatus == Status.READY_STATUS) {
mStatusChannel.setSceneManagerStatus(Status.READY_STATUS);
}
break;
}
mStatus = mStatusChannel.getSceneManagerStatus();
// Update the time.
mMillis = SystemClock.uptimeMillis();
}
}
}
public class Status {
/* Generic Statuses */
public final static int UNKNOWN_STATUS = 0;
public final static int READY_STATUS = 1;
public final static int PAUSED_STATUS = 2;
public final static int QUIT_STATUS = 3;
/* Current statuses values */
private int mSceneManagerStatus = UNKNOWN_STATUS ;
private int mRendererStatus = UNKNOWN_STATUS ;
public synchronized int getSceneManagerStatus() {
return mSceneManagerStatus;
}
public synchronized int getRendererStatus() {
return mRendererStatus;
}
public synchronized void setSceneManagerStatus(int status) {
mSceneManagerStatus = status;
}
public synchronized void setRendererStatus(int status) {
mRendererStatus = status;
}
}
-- EDIT --
This issue happens even with something as simple as this:
public class ThreadActivity extends Activity {
private Booboo mBooboo;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mBooboo = new Booboo();
mBooboo.onCreate();
}
}
public class Booboo implements Runnable {
private Thread mThread;
Booboo() {
mThread = new Thread(this, "SceneManagerThread");
}
public void onCreate() {
Log.d("Booboo", "Thread started");
mThread.start();
}
#Override
public void run() {
while (true) {}
}
}
I know the first reaction is to say that it's the while(true){}. Just remember that this is a contrived example to show the issue. In my own code, I do the lifecycle activity as described in the docs. The issue is that the emulator crashes after some time in an infinite loop like that, whether you have break conditions or not.
You probably want to look into AsyncTask. There is great article here : http://android-developers.blogspot.com/2009/05/painless-threading.html