I am having a bit of difficulty with making an app on Android. I found a few classes to help out with the making, but I can't figure out how to implement things.
The app needs to do the following:
- search for network signals and if none are found after a set interval of time (e.g. five minutes), airplane mode is enabled.
- after another interval of time, airplane mode is disabled and the app searches for Internet signals again.
When searching for info on this, I found three classes that could work, which were Settings (to possibly disable/enable Airplane mode), AlarmManager (recommended on a forum, after I said I found the Timer class), and PhoneStateListener (to detect network signals)
I set up the AlarmManager to where users can specify intervals via the User Interface, but I don't know how to tell if time intervals elapsed or how to properly use PhoneStateListener for this stuff.
The following is the code I have for setting intervals:
package com.android.nman;
import android.app.*;
import android.os.*;
import android.widget.*; // import for ArrayAdapter
import android.view.*; // import for views
import android.widget.AdapterView.*; // import for OnItemClickListener
public class nmanAir extends Activity
{
private PendingIntent aSender;
EditText time;
Button set;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
time = (EditText)findViewById(R.id.time);
set = (Button)findViewById(R.id.set);
}
public void limitAction(View v)
{
// get current time
long now = SystemClock.elapsedRealtime();
String interval = time.getText().toString();
int min = Integer.parseInt(interval);
// schedule alarm
AlarmManager air = (AlarmManager)getSystemService(ALARM_SERVICE);
air.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, now, min*60000, aSender);
// send message
Toast.makeText(nmanAir.this, "Airplane check set to: " + min + " minutes", Toast.LENGTH_LONG).show();
}
}
The Toast stuff is just to check that user input went through correctly, so there is nothing to even suggest I got started on time checking. The class above is supposed to enable Airplane mode. As for the class that disables it, it is similar enough to the above that posting that would be a bit redundant.
The two classes are accessed via the main class that starts one of the classes, depending on selection from a listView layout.
I think you need the AlarmManager to kick off your application again. You can check out the documentation here http://developer.android.com/reference/android/app/AlarmManager.html.
As it notes in the opening though, if the device is rebooted while waiting your application will not be fired however.
[Edit]
Missed the part where you said you were already trying to implement the AlarmManager. Take a look at this thread and the source posted by Mark Murphy. https://groups.google.com/forum/#!topic/android-developers/M7kWB95UltA