2010-09-20 186 views
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。 在此先感謝..

+1

使用logcat獲取錯誤 – 2010-09-20 09:56:44

+0

logcat中沒有錯誤 – 2010-09-20 10:05:02

+0

需要堆棧跟蹤。 – st0le 2010-09-20 10:05:24

回答

0
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, i, PendingIntent.FLAG_ONE_SHOT); 
     Calendar calendar = Calendar.getInstance(); 

     calendar.set(Calendar.SECOND, calendar.get(Calendar.SECOND)+10); 
     AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); 
     alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),pendingIntent); 
0

試試這個,它幫助我,當我有同樣的問題

  1. 連接你的設備,請卸載你的應用程序(如果以前安裝)。
  2. 運行您的應用程序並等待它安裝到您的手機上。
  3. 打開日誌貓,而現在嘗試運行你的應用程序,它根據你強制關閉,日誌貓現在顯示多個紅色和橙色的線條,表明你的錯誤,已線程處理或內存不足等

哪會這個樣子,

This is my error log or log cat

請與錯誤日誌在這裏評論,如果您需要進一步的幫助。