2011-10-03 3 views
2

나는 무슨 일이 생겼을 때 알람을 활성화하는 코드를 작성하려고합니다. 나는 기본 경보를 사용하고 생각할 때 화재를 생각했지만 먼저 경보를 발사하거나 처리하는 방법을 모른다. 둘째 : 소리가 나고 사용자에게 메시지를 보내는 즉각적인 경고를 발사하는 가장 쉬운 방법이 있는지 나는 모른다. 예를 들어안드로이드에서 즉시 알람을 시작하는 방법

: 메시지와 함께 가청 경보는 사용자 다른 일부 다른 코드 도와

감사를 조언 (참) 화재 경우

.

+0

당신이 (소리) 상태 표시 줄에 알림을 의미합니까? –

+0

감사합니다. 그게 내가 찾고 있던거야! :) 그리고 아니, 나는 그것을 상태 표시 줄에 넣고 싶지는 않지만 지금은 괜찮습니다! 감사! – Sonhja

답변

1

코드가 실행 중이고 잘못된 시각을 사용자에게 제공하려는 경우 사운드 만 재생하고 토스트를 표시합니다. 여기 http://developer.android.com/reference/android/app/NotificationManager.html

을 수행 할 수 있습니다 : 당신이 그것으로 갈 알림을 원하는 경우 http://developer.android.com/guide/topics/media/index.html

는 다음을 살펴 : 소리를 들어 http://developer.android.com/guide/topics/ui/notifiers/toasts.html

Toast toast = Toast.makeText(context, myMessage, Toast.LENGTH_LONG).show(); 

: 텍스트 메시지에 대한

알림 개체를 전달하여 알림을 발생시킵니다.

Notification.Builder builder = new Notification.Builder(context); 
builder.setSound(Uri.fromFile(yourFile)); 
builder.setTicker(yourMessage); 
NotificationManager.notify(1,builder.getNotification()); 
+0

완벽하게 작동하지만 ... 화면이나 사격 이벤트를 터치 할 때까지 토스트가 사라지지 않게하려면 어떨까요? – Sonhja

+1

음, 토스트는 일정 기간 동안 표시된 작은 문자 메시지입니다. 좀 더 끈기있는 것을 원하면 Dialog! 더 까다 롭지 만 더 많은 자유를 누릴 수 있습니다. –

+0

그래, 그렇게 할거야. 감사! – Sonhja

3

알람 관리기 :

public void startAlert() { 
Intent intent = new Intent(this, MyBroadcastReceiver.class); 
PendingIntent pendingIntent = PendingIntent.getBroadcast(
     this.getApplicationContext(), 234324243, intent, 0); 
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); 
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() 
     + (i * 1000), pendingIntent); 
Toast.makeText(this, "Alarm set in " + i + " seconds", 
     Toast.LENGTH_LONG).show(); 
} 

브로드 캐스트 리시버 :

public class MyBroadcastReceiver extends BroadcastReceiver { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
    Toast.makeText(context, "Don't panik but your time is up!!!!.", 
     Toast.LENGTH_LONG).show(); 
// Vibrate the mobile phone 
    Vibrator vibrator = (Vibrator)  

     context.getSystemService(Context.VIBRATOR_SERVICE); 
vibrator.vibrate(2000); 
} 


} 
관련 문제