2016-05-26 8 views
0

私はロケーションアラームのデモを実装しており、目的地に到着したときに通知を受け取ります。しかし、私は単純な通知の代わりにアラームを使用したい、私は警報サービスについては知らないし、今まで私はそれを実装していない。ここで目的地に到着したらアラームを設定する方法は?

は、通知を得るための私のコードです:

public class AreWeThereIntentService extends IntentService { 
    private final String TAG = AreWeThereIntentService.class.getName(); 
    private SharedPreferences prefs; 
    private Gson gson; 
    public AreWeThereIntentService() { 
     super("AreWeThereIntentService"); 
    } 

    @Override 
    protected void onHandleIntent(Intent intent) { 
     prefs = getApplicationContext().getSharedPreferences(
       Constants.SharedPrefs.Geofences, Context.MODE_PRIVATE); 
     gson = new Gson(); 
     // 1. Get the event 
     GeofencingEvent event = GeofencingEvent.fromIntent(intent); 
     if (event != null) { 
      if (event.hasError()) { 
       onError(event.getErrorCode()); 
      } else { 
       // 2. Get the transition type 
       int transition = event.getGeofenceTransition(); 
       if (transition == Geofence.GEOFENCE_TRANSITION_ENTER || 
         transition == Geofence.GEOFENCE_TRANSITION_DWELL || 
         transition == Geofence.GEOFENCE_TRANSITION_EXIT) { 
        List<String> geofenceIds = new ArrayList<>(); 
        // 3. Accumulate a list of event geofences 
        for (Geofence geofence : event.getTriggeringGeofences()) { 
         geofenceIds.add(geofence.getRequestId()); 
        } 
        if (transition == Geofence.GEOFENCE_TRANSITION_ENTER || 
          transition == Geofence.GEOFENCE_TRANSITION_DWELL) { 
         // 4. Pass the geofence list to the notification method 
         onEnteredGeofences(geofenceIds); 
        } 
       } 
      } 
     } 
    } 

    private void onEnteredGeofences(List<String> geofenceIds) { 

     // 1. Outer loop over all geofenceIds 
     for (String geofenceId : geofenceIds) { 
      String geofenceName = ""; 
      // 2, Loop over all geofence keys in prefs and retrieve NamedGeofence from SharedPreferences 
      Map<String, ?> keys = prefs.getAll(); 
      for (Map.Entry<String, ?> entry : keys.entrySet()) { 
       String jsonString = prefs.getString(entry.getKey(), null); 
       NamedGeofence namedGeofence = gson.fromJson(jsonString, NamedGeofence.class); 
       if (namedGeofence.id.equals(geofenceId)) { 
        geofenceName = namedGeofence.name; 
        break; 
       } 
      } 
      // 3. Set the notification text and send the notification 
      String contextText = 
        String.format(this.getResources().getString(R.string.Notification_Text), geofenceName); 
      // 1. Create a NotificationManager 
      NotificationManager notificationManager = 
        (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE); 

      Intent intent = new Intent(this, MapsActivity.class); 
      intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP); 
      PendingIntent pendingNotificationIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); 

      Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
      long[] pattern = {0, 100, 200, 300}; 
      // 3. Create and send a notification 
      Notification notification = new NotificationCompat.Builder(this) 
        .setSmallIcon(R.mipmap.map_marker_icon) 
        .setContentTitle(this.getResources().getString(R.string.Notification_Title)) 
        .setContentText(contextText) 
        .setContentIntent(pendingNotificationIntent) 
        .setStyle(new NotificationCompat.BigTextStyle().bigText(contextText)) 
        .setPriority(NotificationCompat.PRIORITY_HIGH) 
        .setAutoCancel(true) 
        .setSound(alarmSound) 
        .setVibrate(pattern) 
        .build(); 

      notificationManager.notify(0, notification); 
     } 
    } 

    private void onError(int i) { 
     Log.e(TAG, "Geofencing Error: " + i); 
    } 
} 

私が欲しい私はジオフェンスを入力したときに代わりに簡単な通知のアラームです。私は右のあなたの質問を得た場合

+0

あなた自身で最初にアラーム(ジオフェンスなし)を実装してみてください。それはあなた自身の質問に答えるのに十分であるはずです。 –

+0

メイト、あなたの質問は「アラームを設定する方法」を参照しています。 – Blackkara

+0

@ cricket_007はい、誰も私の質問に答えることはできません。 –

答えて

0

... ...

Intent myIntent = new Intent(this, AlarmReceiver.class); 

          PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, myIntent, PendingIntent.FLAG_UPDATE_CURRENT); 
          alarmManager.set(AlarmManager.RTC_WAKEUP, 1, pendingIntent); 
          alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, randomValue, 0,pendingIntent); 

とAlarmReceiverでのアラームのアラームマネージャを使用して、このような放送受信機を通じて通知を送信

public class Silent_notification_receiver extends BroadcastReceiver { 

    @Override 
    public void onReceive(Context context, Intent intent) { 

     NotificationManager mNotificationManager = (NotificationManager) context 
       .getSystemService(Context.NOTIFICATION_SERVICE); 

     NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
       context).setSmallIcon(R.drawable.app_icon) 
       .setAutoCancel(true).setContentTitle("Alert") 
       .setContentText("You are in Silent Area"); 
     mBuilder.setAutoCancel(true); 
     mNotificationManager.notify(0, mBuilder.build()); 
    } 
0

あなたはほとんどそこにありますアラームや電話のような動作が必要な場合は、アプリケーションのアクティビティ/フラグメント/ダイアログを開いて着信音を鳴らす必要があります。 https://developer.android.com/reference/android/media/RingtoneManager.html
https://developer.android.com/reference/android/media/Ringtone.html

+0

あなたが話していることについて私は少しデモをすることができます –

+0

これは可能な有用なhttp: /www.concretepage.com/android/android-ringtone-and-ringtonemanager-example-get-default-and-current-ringtoneテストされていません。 –

+0

ok ..今チェックしています –

関連する問題