1
我正在使用下面的鬧鐘代碼,它會在10秒後發出警報。它工作正常在模擬器但在實際設備的forcely closing..can你指導我在正確的方式.. 這是我actvity ..android中的鬧鐘
public class Alarm1 extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent intent = new Intent(this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this,001000,intent,0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (5 * 1000), pendingIntent);
Toast.makeText(this,"Alarm set", Toast.LENGTH_LONG).show();
} }` 這是接收器類
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Alarm worked", Toast.LENGTH_LONG).show();
try{
Intent back = new Intent(context,Alarm.class);
back.addFlags(Intent.FLAG_FROM_BACKGROUND);
back.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
back.setAction("android.intent.action.MAIN");
back.addCategory("android.intent.category.LAUNCHER");
context.startActivity(back);
}
catch(Exception e){}
}
}
這是爲鈴聲播放鬧鈴。
public class Alarm extends Activity {
private MediaPlayer mp; /** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.alarm);
mp = MediaPlayer.create(this, R.raw.up);
mp.start();
Button start = (Button) findViewById(R.id.Button01);
start.setOnClickListener(vvvvv);
}
private OnClickListener vvvvv = new OnClickListener() {
public void onClick(View v) {
mp.stop();
finish();
}
};
}
這是工作在模擬器罰款也真實device..i使用權限.. 我使用的三星Galaxy 1.6。 在此先感謝..
使用logcat獲取錯誤 – 2010-09-20 09:56:44
logcat中沒有錯誤 – 2010-09-20 10:05:02
需要堆棧跟蹤。 – st0le 2010-09-20 10:05:24