Reputation: 157
I want to develop an app. The app has a spinner dropdown. The dropdown has 3 option (30min, 1h, 2h). When for example 30min is pressed I want to start an elapsed Real Time for 30min and after that I want to turn off wifi, that means after 30min the wifi should be turned off.
The logcat shows me a NullPointer Exception.
Here is my code
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;
And in this class the wifi should be managed (turned on/off)
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();
}
}
Error of the 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)
The error is on this line:
PendingIntent pendingIntent = PendingIntent.getBroadcast(SpinnerTimeOnItemSelectedListener.this, 0, intent, 0);
How can I fix this problem? Thanks
Upvotes: 0
Views: 14527
Reputation: 2140
Here is little bit concept problem:
elapsedRealtime() and elapsedRealtimeNanos() return the time since the system was booted, and include deep sleep. This clock is guaranteed to be monotonic, and continues to tick even when the CPU is in power saving modes, so is the recommend basis for general purpose interval timing.
check : http://developer.android.com/reference/android/os/SystemClock.html
and here your task you can done with the Chronometer class. You can bind it to SystemClock.elapsedRealtime() using setBase(). Another way could be if you use Handler inside an Activity and using the sendEmptyMessageDelayed() method with value 1000*60*30(30 minutes) to update your timer.
Or if you Want to use pending intent Then you have to
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
Thats it....
Upvotes: 4