我想开发一款应用。该应用有一个微调下拉列表。下拉列表有3个选项(30分钟,1小时,2小时)。例如,当按下30分钟时,我想开始经过30分钟的实时时间,之后我想关闭wifi,这意味着在30分钟后应该关闭wifi。
logcat向我显示NullPointer异常。
这是我的代码
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.SystemClock;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Toast;
public class SpinnerTimeOnItemSelectedListener extends Activity implements AdapterView.OnItemSelectedListener {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long
if(parent.getItemAtPosition(position).toString().equals("Zeit auswählen") || parent.getItemAtPosition(position).toString().equals("Select Time")){
onNothingSelected(parent);
} else if (parent.getItemAtPosition(position).toString().equals("30min")){
Intent intent = new Intent(SpinnerTimeOnItemSelectedListener.this, ConnectionManager.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(SpinnerTimeOnItemSelectedListener.this, 0, intent, 0);
long firstTime = SystemClock.elapsedRealtime();
AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
am.setRepeating(AlarmManager.ELAPSED_REALTIME, firstTime, 5000, pendingIntent);
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// Wenn es einen ElapsedRealTimeAlarm gibt, soll er gecancelt werden
// Ansonsten nichts
;
}
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
在这个课程中,应该管理wifi(开启/关闭)
public class ConnectionManager extends BroadcastReceiver {
/**
*
* @param context The Context in which the receiver is running.
* @param intent The Intent being received.
*/
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "I'm running", Toast.LENGTH_SHORT).show();
}
}
logcat的错误:
07-24 17:35:17.503 5430-5430/com.falkenherz.abusufean.batterycooler E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.falkenherz.abusufean.batterycooler, PID: 5430
java.lang.NullPointerException
at android.content.ContextWrapper.getContentResolver(ContextWrapper.java:104)
at android.app.PendingIntent.getService(PendingIntent.java:522)
at com.example.ali.turnoffwifi.SpinnerTimeOnItemSelectedListener.onItemSelected(SpinnerTimeOnItemSelectedListener.java:42)
at android.widget.AdapterView.fireOnSelected(AdapterView.java:956)
at android.widget.AdapterView.access$200(AdapterView.java:49)
at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:920)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5356)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
at dalvik.system.NativeStart.main(Native Method)
错误在这一行:
PendingIntent pendingIntent = PendingIntent.getBroadcast(SpinnerTimeOnItemSelectedListener.this, 0, intent, 0);
如何解决此问题? 感谢
答案 0 :(得分:4)
这是一点概念问题:
elapsedRealtime()和elapsedRealtimeNanos()返回自系统启动以来的时间,并包括深度睡眠。该时钟保证是单调的,即使在CPU处于省电模式时也会继续打勾,因此是通用间隔时序的推荐基础。
check : http://developer.android.com/reference/android/os/SystemClock.html
在这里,您可以使用Chronometer类完成任务。您可以使用setBase()将其绑定到SystemClock.elapsedRealtime()。另一种方法是,如果在Activity中使用Handler并使用值为1000 * 60 * 30(30分钟)的sendEmptyMessageDelayed()方法来更新计时器。
或者如果你想使用待定意图那么你必须
1)getcurrenttime in Millis using calenderinstance
2)convert 30 minutes to millis and
3) Total millis =currenttimeinmillis+30minutes in millis
then set your Total millis to alarm manager object
多数民众赞成......