[align = left] I want to be reminded alarm clock , set the time in EssayActivity , start the service , access to the system time , the two are equal the alarm . But the service has been wrong ( abnormal ) Calling a method in the system process without a qualified user: android.app.ContextImpl.sendBroadcast: 1140 android.content.ContextWrapper.sendBroadcast : 345 com.android.settings.batterywarning.ReadCodeTask.sendWarningMessage: 59 com.android.settings.batterywarning.ReadCodeTask.run: 41 java.util.Timer $ TimerImpl.run: 284
commissioning has been off from timer.scheduleAtFixedRate (new TimerTask () { Skip to end } , 0,1000 * 60 ) ;
attach source code lines and surfaces , the ball great God enlighten !
package com.example.notepad;
import java.util.Timer;
import java.util.TimerTask;
import android.app.AlertDialog;
import android.app.Service;
import android.content.DialogInterface;
import android.content.Intent;
import android.hardware.SensorManager;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.IBinder;
import android.os.Vibrator;
import android.text.format.Time;
import android.util.Log;
public class EssayService extends Service {
private static final String TAG = "EssayService";
private SensorManager mgr ;/ / sensor
private Vibrator vibrator ;/ / vibration motor
private SoundPool soundPool ;/ / sound playback object
private int streamID = 0 ;/ / audio stream
private Timer timer;
public static double year = 0;
public static double month = 0;
public static double day = 0;
public static double hour = 0;
public static double minute = 0;
@ Override
public void onCreate () {
/ / TODO Auto-generated method stub
super.onCreate ();
/ / initialize audio
soundPool = new SoundPool (10, AudioManager.STREAM_SYSTEM, 5);
soundPool.load (this, R.raw.kid, 1);
/ / get sensor
mgr = (SensorManager) getSystemService (SENSOR_SERVICE);
vibrator = (Vibrator) getSystemService (VIBRATOR_SERVICE);
year = EssayActivity.year;
month = EssayActivity.month;
day = EssayActivity.day;
hour = EssayActivity.hour;
minute = EssayActivity.minute;
}
@ Override
public void onDestroy () {
/ / TODO Auto-generated method stub
/ / Service audio playback stops at the end
if (streamID! = 0) {
soundPool.stop (streamID);
}
if (timer! = null) {
timer.cancel ();
}
super.onDestroy ();
}
@ Override
public int onStartCommand (Intent intent, int flags, int startId) {
/ / TODO Auto-generated method stub
Log.i (TAG, " service start " ) ;
/ / start the service
if (timer! = null) {
timer.cancel ();
}
timer = new Timer ();
timer.scheduleAtFixedRate (new TimerTask () {
@ Override
public void run () {
/ / TODO Auto-generated method stub
/ / Get the current system the same time
Time t = new Time ();
t.setToNow ();
double tYear = t.year;
double tMonth = t.month;
double tDay = t.monthDay;
double tHour = t.hour;
double tMinute = t.minute;
if (year == tYear && month == tMonth && day == tDay
&& hour == tHour && minute == tMinute) {
/ / vibration, play a sound
vibrator.vibrate (1000);
if (streamID == 0) {
streamID = soundPool.play (1, 1, 1, 0, -1, 1);
}
new AlertDialog.Builder (EssayService.this)
. setTitle (" whether to cancel ? " )
. setNegativeButton (" yes ", new DialogInterface.OnClickListener () {
@ Override
public void onClick (DialogInterface dialog, int which) {
/ / TODO Auto-generated method stub
if (timer! = null) {
timer.cancel ();
timer.purge ();
}
}
})
. setNegativeButton (" Cancel ", new DialogInterface.OnClickListener () {
@ Override
public void onClick (DialogInterface dialog, int which) {
/ / TODO Auto-generated method stub
}
})
. show ();
}
}
}, 0,1000 * 60);
return super.onStartCommand (intent, flags, startId);
}
@ Override
public IBinder onBind (Intent intent) {
/ / TODO Auto-generated method stub
return null;
}
}
Here is the code from EssayActivity to service
case R.id.btnClock:
LayoutInflater inflater = getLayoutInflater ();
final View layout = inflater.inflate (R.layout.timeset,
(ViewGroup) findViewById (R.id.dialog));
final EditText passText = new EditText (this);
passText.setInputType (InputType.TYPE_CLASS_TEXT);
new AlertDialog.Builder (EssayActivity.this)
. setTitle (" Please enter the date of presentation : " )
. setView (layout)
. setPositiveButton (" OK ", new DialogInterface.OnClickListener () {
@ Override
public void onClick (DialogInterface dialog, int which) {
LayoutInflater factory = LayoutInflater.from (log.this);
final View textEntryView = factory.inflate (R.layout.dialog, null);
EditText editYear = (EditText) layout.findViewById (R.id.editYear);
EditText editMonth = (EditText) layout.findViewById (R.id.editMonth);
EditText editDay = (EditText) layout.findViewById (R.id.editDay);
EditText editHour = (EditText) layout.findViewById (R.id.editHour);
EditText editMinite = (EditText) layout.findViewById (R.id.editMinite);
try {
year = Double.parseDouble (editYear.getText (). toString ());
month = Double.parseDouble (editMonth.getText (). toString ());
day = Double.parseDouble (editDay.getText (). toString ());
hour = Double.parseDouble (editHour.getText (). toString ());
minute = Double.parseDouble (editMinite.getText (). toString ());
} catch (Exception e) {
e.printStackTrace ();
} [align = left]
saveEssayPassword ();
Intent intent = new Intent (EssayActivity.this, EssayService.class);
startService (intent);
}
})
. setNegativeButton (" Cancel ", [/ align] new DialogInterface.OnClickListener () {
------ Solution ----------- ---------------------------------
slightly Diao look !
没有评论:
发表评论