在Android中,经过一段时间后,我如何做一些事情?
我是编程新手 我想在2小时后关闭wifi我知道如何在后台关闭wifi 我在谷歌上搜索发现,需要一个真正的计时器。我还找到了这段代码并实现了它这是我的类中唯一的代码当用户从微调器下拉列表中选择某个内容时调用这个类:在Android中,经过一段时间后,我如何做一些事情?,android,sdk,elapsedtime,Android,Sdk,Elapsedtime,我是编程新手 我想在2小时后关闭wifi我知道如何在后台关闭wifi 我在谷歌上搜索发现,需要一个真正的计时器。我还找到了这段代码并实现了它这是我的类中唯一的代码当用户从微调器下拉列表中选择某个内容时调用这个类: import android.app.Activity; import android.app.AlarmManager; import android.app.PendingIntent; import android.content.Intent; import android.o
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.os.SystemClock;
import android.widget.Toast;
/**
* Wird aufgerufen, wenn eine Zeit von der dropdown liste gewählt wurde
*/
public class ElapsedRealtimeAlarm extends Activity {
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
PendingIntent mAlarmSender = PendingIntent.getService(ElapsedRealtimeAlarm.this,
0, new Intent(ElapsedRealtimeAlarm.this, ElapsedRealtimeAlarm.class), 0);
long firstTime = SystemClock.elapsedRealtime(); // elapsedRealTime --> Zeit seitdem booten.
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.setRepeating(AlarmManager.ELAPSED_REALTIME, // die 2h (1*1000*3600*2) werden von der Bootzeit(firstTime) dazugerechnet
firstTime, 1*1000*3600*2, mAlarmSender); // 1*1000 --> 1s * 3600 --> 1h * 2 --> 2h
Toast.makeText(getApplicationContext(), "Das ist ein Text", Toast.LENGTH_SHORT).show();
}
}
代码还可以吗?在计时器运行后,如何关闭wifi?
这是计时器只运行了一次还是像一个间隔
对不起,我的英语不好
谢谢
编辑:
我完成了Deb回答中提到的步骤,但仍然没有发生任何事情
这是密码
步骤1:制作一个BroadcatReceiver扩展到WakeFulBroadCatReceiver
步骤2:创建扩展IntentService类的服务
第3步:在OnHandle内的服务中,编写关闭或打开wifi的代码
import android.app.IntentService;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.net.wifi.WifiManager;
import android.view.View;
import android.widget.Toast;
public class BackgroundService extends IntentService {
public BackgroundService() {
super("BackgroundService");
}
@Override
protected void onHandleIntent(Intent intent) {
wifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
if (wifiManager.isWifiEnabled()) {
wifiManager.setWifiEnabled(false);
} else {
wifiManager.setWifiEnabled(true);
}
}
第4步:现在回到BroadcastReceiver类,在第2步中创建的onReceive call服务中,代码已经在第1步中了
清单文件接收者、wifi服务和权限
<manifest ..... ..... ....>
<application>
.....
<receiver android:name=".BroadCastReceiver" ></receiver>
<service android:name=".BackgroundService" android:exported="false"></service>
</application>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
</manifest>
ElapsedRealtimeAlarm的onCreate
做下列事情对你很有用
创建一项服务,将wifi和其他操作
在onReceive方法内创建广播接收器,调用服务
创建一个挂起的意图并在您想要的时间内发出警报,当警报时间段消失时,然后触发广播接收器
制作一个BroadcatReceiver扩展到一个BroadcatReceiver。
创建一个扩展IntentService类的服务。
在OnHandle内的服务中,请输入关闭或打开wifi的代码。
现在回到您的BroadcastReceiver类,在第2步中的onReceive调用您的服务。
你的代码行
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.setRepeating(AlarmManager.ELAPSED_REALTIME, // die 2h (1*1000*3600*2) werden von der Bootzeit(firstTime) dazugerechnet
firstTime, 1*1000*3600*2, mAlarmSender);
这负责在每2小时后重复运行alarmmanager。但如果您希望alarm manager仅运行一次,请将am.SETREPENDING替换为am.set
你的代码
PendingIntent mAlarmSender = PendingIntent.getService(ElapsedRealtimeAlarm.this,
0, new Intent(ElapsedRealtimeAlarm.this, ElapsedRealtimeAlarm.class), 0);
用PendingEvent.getBroadcast替换PendingEvent.getService,并传递在步骤1中创建的BroadcastReceiver类的引用
注意:不要忘记在您的清单中写入接收方和服务,否则它将无法工作
更新:
在您的BroadcastReceive的onReceive呼叫服务中,如下所示
Intent service=new Intent(context,BackgroundService.class);
startWakefulService(context, service);
这将称为onHandleIntent
更新2:
用此替换getBaseContext,并将PendingEvent.getService更改为
Intent i=new Intent(this, BroadCastReceiver.class);
PendingIntent alarmIntent=PendingIntent.getBroadcast(this, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
好啊只是为了确保:我写了关闭wifi的方法。然后我将编写一个广播接收器,在其中调用关闭wifi的方法。然后我在我的ElapsedRealTimeAlarm类中调用广播接收器,在时间流逝之后?这就是你提到的吗?我的吊坠帐篷还好吗?但这仍然不能回答我的问题。我怎么知道“消逝”实时警报的时间已经过去了?我试过了,但没用。他先停下来:非常感谢你的回答!我试过了,但什么也没发生。我应该发布代码吗?我也在清单文件中写了代码。此外,打开wifi/offI的权限根据您的步骤更新了我的代码。请检查一下。感谢她停止了应用程序,并在这行给了我一个NullPointerException:startActivitynew Intents PinnertimeOnItemSelectedListener.this,ElapsedRealtimeAlarm.class;这条线错了吗?我是否应仅使用新的ElapsedRealTimeAlarm调用ElapsedRealTimeAlarm?您是否可以共享ElapsedRealTimeAlarm的onCreate和执行startActivitynew IntentsPinnertTimeOnItemSelectedListener的方法。这是ElapsedRealTimeAlarm.class;
PendingIntent mAlarmSender = PendingIntent.getService(ElapsedRealtimeAlarm.this,
0, new Intent(ElapsedRealtimeAlarm.this, ElapsedRealtimeAlarm.class), 0);
Intent service=new Intent(context,BackgroundService.class);
startWakefulService(context, service);
Intent i=new Intent(this, BroadCastReceiver.class);
PendingIntent alarmIntent=PendingIntent.getBroadcast(this, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);