让应用程序在设定的时间间隔后启用机场模式?
我在 Android 上制作应用程序时遇到了一些困难。我找到了一些课程来帮助制作,但我不知道如何实现。
该应用程序需要执行以下操作:
- 搜索网络信号,如果在设定的时间间隔(例如五分钟)后找不到网络信号,则启用飞行模式。
- 再过一段时间后,飞行模式将被禁用,应用程序会再次搜索互联网信号。
在搜索这方面的信息时,我发现了三个可以工作的类,它们是 Settings(可能禁用/启用飞行模式)、AlarmManager(在我说我找到了 Timer 类之后在论坛上推荐)和 PhoneStateListener(用于检测网络信号)
我将 AlarmManager 设置为用户可以通过用户界面指定时间间隔,但我不知道如何判断时间间隔是否已过,也不知道如何正确使用 PhoneStateListener 来处理这些内容。
以下是我用于设置间隔的代码:
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();
}
}
Toast 的内容只是检查用户输入是否正确,因此没有任何内容表明我开始按时检查。上面的类应该启用飞行模式。至于禁用它的类,它与上面的帖子非常相似,但有点多余。
这两个类可通过启动其中一个类的主类来访问,具体取决于 listView 布局中的选择。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您需要 AlarmManager 来再次启动您的应用程序。您可以在此处查看文档 http://developer.android.com/reference/ android/app/AlarmManager.html。
正如开头所指出的,如果设备在等待时重新启动,您的应用程序将不会被触发。
[编辑]
错过了您所说的已经尝试实现 AlarmManager 的部分。看看这个帖子和 Mark Murphy 发布的来源。 https://groups.google.com/forum/#!topic/android -开发者/M7kWB95UltA
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