2016-07-12 10 views
0

家庭、オフィス、遊び場の3つの場所を考えてみましょう。その場所に出入りするたびに、メッセージアラートが表示されます。 これはMainActivity.java私は、複数の近接ポイント を追加私の主な活動である複数のプロキシミティアラートをブロードキャスト受信機で設定するandroid

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
    fab.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) 
        .setAction("Action", null).show(); 
     } 
    }); 
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 

    latitudeEditText = (EditText) findViewById(R.id.point_latitude); 
    longitudeEditText = (EditText) findViewById(R.id.point_longitude); 
    addAlertButton = (Button) findViewById(R.id.add_alert_button); 
    double latitude = 43.039, latitude1 = 43.039; 
    double longitude = 60.238, longitude1 = 60.239; 
    addProximityAlert(latitude, longitude); 
    addProximityAlert(latitude1, longitude1); 

    Toast.makeText(getApplicationContext(), "Alert Added", Toast.LENGTH_SHORT).show(); 
} 

private void addProximityAlert(double latitude,double longitude) { 


    Intent intent = new Intent(PROX_ALERT_INTENT); 
    intent.putExtra("lat",latitude); 
    intent.putExtra("long",longitude); 
    PendingIntent proximityIntent = PendingIntent.getBroadcast(this, (int) System.currentTimeMillis(), intent,PendingIntent.FLAG_CANCEL_CURRENT); 
    Toast.makeText(this,Long.toString(System.currentTimeMillis()),Toast.LENGTH_LONG).show(); 
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 

     return; 
    } 
    locationManager.addProximityAlert(latitude, 
      longitude, 
      POINT_RADIUS, 
      PROX_ALERT_EXPIRATION, 
      proximityIntent 
    ); 
    IntentFilter filter = new IntentFilter(PROX_ALERT_INTENT); 
    registerReceiver(new ProximityIntentReceiver(), filter); 

} 

//これは私が

public class ProximityIntentReceiver extends BroadcastReceiver { 
    private static final int NOTIFICATION_ID = 1000; 
    double lat,lng; 
    String d=""; 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     String key = LocationManager.KEY_PROXIMITY_ENTERING; 
     Boolean entering = intent.getBooleanExtra(key, false); 
     lat=intent.getDoubleExtra("lat",0.00); 
     lng=intent.getDoubleExtra("long",0.00); 
     if (entering) { 
      Log.d(getClass().getSimpleName(), "entering"); 
      d="Entering region"+Double.toString(lat)+Double.toString(lng); 
      Toast.makeText(context, d, Toast.LENGTH_SHORT).show(); 
     }else { 
      Log.d(getClass().getSimpleName(), "exiting"); 
      d= "Exiting region"+Double.toString(lat)+Double.toString(lng); 
      Toast.makeText(context,d, Toast.LENGTH_SHORT).show(); 

     } 

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

     Intent notificationIntent = new Intent(context, MainActivity.class); 
     PendingIntent pendingIntent = PendingIntent.getActivity(context, (int) System.currentTimeMillis(), notificationIntent, 0); 
     Notification n = new Notification.Builder(context) 
       .setContentTitle("Proximity") 
       .setContentText("Subject: "+d) 
       .setSmallIcon(R.drawable.hell) 
       .setContentIntent(pendingIntent) 
       .setAutoCancel(true) 
       .build(); 
     notificationManager.notify((int) System.currentTimeMillis(), n); 

    } 


} 
PendingIntentReceiver.java
を入力し、既存のアラート 通知私の保留中の意図受信機であります
+0

あなたの質問は何ですか? – Arjun

+0

私はそれのためのコードが必要です! – Senthil

+1

あなたはなぜあなたにコードする時間があると思いますか!!!!! – Arjun

答えて

0

試用:

Intent notificationIntent = new Intent(context, MainActivity.class); 
notificationIntent.setAction(Long.toString(System.currentTimeMillis())); 

TaskStackBuilder stackBuilder = TaskStackBuilder.create(context); 
stackBuilder.addParentStack(MainActivity.class); 
stackBuilder.addNextIntent(intent); 

PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); 

Notification n = new Notification.Builder(context) 
     .setContentTitle("Proximity") 
     .setContentText("Subject: "+d) 
     .setSmallIcon(R.drawable.hell) 
     .setContentIntent(pendingIntent) 
     .setAutoCancel(true) 
     .build(); 
notificationManager.notify((int) System.currentTimeMillis(), n); 
関連する問題