Android位置警报通知

时间:2014-08-31 15:36:34

标签: android android-edittext android-notifications

我正在开发一个名为Location Alarm的基于android的应用程序,如果它进入特定位置,将触发警报并显示通知。但现在我想让它在通知中显示用户输入的注释。任何人都可以帮助我。?

以下是MainActivity的代码

public void onMapClick(LatLng point) {  
    locationCount++;
    // Drawing marker on the map
    drawMarker(point);          
    drawCircle(point);          

    // Creating an instance of ContentValues
    ContentValues contentValues = new ContentValues();

    // Setting latitude in ContentValues
    contentValues.put(LocationsDB.FIELD_LAT, point.latitude );

    // Setting longitude in ContentValues
    contentValues.put(LocationsDB.FIELD_LNG, point.longitude);

    // Setting zoom in ContentValues
    contentValues.put(LocationsDB.FIELD_ZOOM, googlemap.getCameraPosition().zoom);

    // Creating an instance of LocationInsertTask
    LocationInsertTask insertTask = new LocationInsertTask();

    // Storing the latitude, longitude and zoom level to SQLite database
    insertTask.execute(contentValues);                

    Toast.makeText(getBaseContext(), "Marker is added to the Map", Toast.LENGTH_SHORT).show();  
 // This intent will call the activity ProximityActivity

    Intent contentValues1 = new Intent("com.example.locationdetectormechanism.activity.alert");

    // Passing latitude to the PendingActivity
    contentValues1.putExtra("lat",point.latitude);


    // Passing longitude to the PendingActivity
    contentValues1.putExtra("lng", point.longitude);
    contentValues1.putExtra("note",R.id.et1);

    // Creating a pending intent which will be invoked by LocationManager when the specified region is
    // entered or exited
    pendingIntent = PendingIntent.getActivity(getBaseContext(), 0, contentValues1,Intent.FLAG_ACTIVITY_NEW_TASK);


    // Setting proximity alert 
    // The pending intent will be invoked when the device enters or exits the region 20 meters
    // away from the marked point
    // The -1 indicates that, the monitor will not be expired
    locationManager.addProximityAlert(point.latitude, point.longitude, 20, -1, pendingIntent);              

    /** Opening the editor object to write data to sharedPreferences */
    SharedPreferences.Editor editor = sharedPreferences.edit();

    // Storing the latitude for the i-th location
    editor.putString("lat"+ Integer.toString((locationCount-1)), Double.toString(point.latitude));

    // Storing the longitude for the i-th location
    editor.putString("lng"+ Integer.toString((locationCount-1)), Double.toString(point.longitude));

    // Storing the count of locations or marker count
    editor.putInt("locationCount", locationCount);

    /** Storing the zoom level to the shared preferences */
    editor.putString("zoom", Float.toString(googlemap.getCameraPosition().zoom));               

    /** Saving the values stored in the shared preferences */
    editor.commit();                

   //Toast.makeText(getBaseContext(), "Proximity Alert is added", Toast.LENGTH_SHORT).show();        

}

以下是我的AlertActivity

public class AlertActivity extends Activity {

    String notificationTitle;
    String notificationContent;
    String tickerMessage;
    String tickerMessage1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);


        boolean proximity_entering = getIntent().getBooleanExtra(LocationManager.KEY_PROXIMITY_ENTERING, true);

        double lat = getIntent().getDoubleExtra("lat", 0);

        double lng = getIntent().getDoubleExtra("lng", 0);
        String note = getIntent().getStringExtra("note");

        String strLocation = Double.toString(lat)+","+Double.toString(lng);

        if(proximity_entering){
            Toast.makeText(getBaseContext(),"Entering the region"  ,Toast.LENGTH_LONG).show();
            notificationTitle = "Proximity - Entry";
            notificationContent = "Entered the region: " + strLocation;
            tickerMessage = "Entered the region: " + strLocation;
            tickerMessage1 = "Note :" + R.id.et1;
        }else{
            Toast.makeText(getBaseContext(),"Exiting the region"  ,Toast.LENGTH_LONG).show();
            notificationTitle = "Proximity - Exit";
            notificationContent = "Exited the region: " + strLocation;
            tickerMessage = "Exited the region :" + strLocation;
        }



        Intent notificationIntent = new Intent(getApplicationContext(),NotificationView.class);

        /** Adding content to the notificationIntent, which will be displayed on 
         * viewing the notification
         */
        notificationIntent.putExtra("content", notificationContent );

        /** This is needed to make this intent different from its previous intents */
        notificationIntent.setData(Uri.parse("tel:/"+ (int)System.currentTimeMillis()));

        /** Creating different tasks for each notification. See the flag Intent.FLAG_ACTIVITY_NEW_TASK */
        PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, notificationIntent, Intent.FLAG_ACTIVITY_NEW_TASK);

        /** Getting the System service NotificationManager */
        NotificationManager nManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);

        /** Configuring notification builder to create a notification */
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(getApplicationContext())
                            .setWhen(System.currentTimeMillis())
                            .setContentText(notificationContent)
                            .setContentTitle(notificationTitle)
                            .setSmallIcon(R.drawable.ic_launcher)
                            .setAutoCancel(true)
                            .setTicker(tickerMessage)
                            .setContentIntent(pendingIntent)
                            .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
                            .setVibrate(new long[] { 1000, 1000, 1000, 1000, 1000 });


        /** Creating a notification from the notification builder */
        Notification notification = notificationBuilder.build();

        /** Sending the notification to system. 
         * The first argument ensures that each notification is having a unique id 
         * If two notifications share same notification id, then the last notification replaces the first notification 
         * */
        nManager.notify((int)System.currentTimeMillis(), notification);

        /** Finishes the execution of this activity */
        finish();
}

0 个答案:

没有答案