2013-01-19 36 views
2

即時通訊嘗試實施基於GPS達的鬧鐘如果我在城市x我安置一個針點在該地點時,當我達到特定的精確點它應該給我一個警報或警報我已經實施了一個完整的地圖結構,我也編程爲顯示我的當前位置和準確定位。我不知道如何附上源代碼,請幫助我以這種方式對Android真的很陌生,我不知道該怎麼做。這裏是代碼請指導我在哪裏我錯了。如何設置鬧鐘或谷歌地圖上的警報android

public void onLocationChanged(Location l) 
{ 
// TODO Auto-generated method stub 
lat=(int) (l.getLatitude() *1E6) ; 
longi=(int) (l.getLongitude() *1E6); 
ourLocation= new GeoPoint(lat,longi); 
OverlayItem overlayItem= new OverlayItem(ourLocation,"",""); 
CustomPinpoint custom=new CustomPinpoint(d,Main.this); 
custom.insertPinPoint(overlayItem); 
overlayList.add(custom); 
controller.setCenter(ourLocation); 
    geocoder= new Geocoder(getBaseContext(), Locale.getDefault()); 

    try 
    {  
    List<Address>address1=geocoder.getFromLocation 
    (ourLocation.getLatitudeE6()/1E6,ourLocation .getLongitudeE6()/1E6, 1); 

    if(address1.size()>0) 
    { 
    for(int i=0; i<address1.get(0).getMaxAddressLineIndex();i++) 
    {     
     display1 += address1.get(0).getAddressLine(i)+"\n"; 

    } 

    } 

} 
catch(IOException e1) 
{ 
    e1.printStackTrace(); 
    Toast.makeText(getBaseContext(), "error", Toast.LENGTH_LONG).show(); 
} 

if(display1.equals(display)) 
{ 
    AlertDialog alert= new AlertDialog.Builder(Main.this).create(); 
    alert.setTitle("Destination"); 
    alert.setMessage("You Reached to destination"); 
    alert.setButton("OK",new DialogInterface.OnClickListener() 
{ 


    public void onClick(DialogInterface arg0, int arg1) 
    { 
     // TODO Auto-generated method stub 
     } 
     }); 
    alert.show(); 
     } 
    } 

回答

0

上面您的代碼是不夠的,顯示警告對話框。要在代碼中應用警報,您必須像這樣包含代碼表單;

/** 
    * Function to show settings alert dialog On pressing Settings button will 
    * lauch Settings Options 
    * */ 
    public void showSettingsAlert() { 
     AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext); 

     // Setting Dialog Title 
     alertDialog.setTitle("GPS is settings"); 

     // Setting Dialog Message 
     alertDialog 
       .setMessage("GPS is not enabled. Do you want to go to settings menu?"); 

     // On pressing Settings button 
     alertDialog.setPositiveButton("Settings", 
       new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int which) { 
         Intent intent = new Intent(
           Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
         mContext.startActivity(intent); 
        } 
       }); 

     // on pressing cancel button 
     alertDialog.setNegativeButton("Cancel", 
       new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int which) { 
         dialog.cancel(); 
        } 
       }); 

     // Showing Alert Message 
     alertDialog.show(); 
    } 

而且你必須在你的項目的src /目錄下創建如下的獨立java代碼(類)

下面的類名是「AlertDialogManager.java」。

public class AlertDialogManager { 
    /** 
    * Function to display simple Alert Dialog 
    * @param context - application context 
    * @param title - alert dialog title 
    * @param message - alert message 
    * @param status - success/failure (used to set icon) 
    *    - pass null if you don't want icon 
    * */ 
    public void showAlertDialog(Context context, String title, String message, 
      Boolean status) { 
     AlertDialog alertDialog = new AlertDialog.Builder(context).create(); 

     // Setting Dialog Title 
     alertDialog.setTitle(title); 

     // Setting Dialog Message 
     alertDialog.setMessage(message); 

     if(status != null) 
      // Setting alert dialog icon 
      alertDialog.setIcon((status) ? R.drawable.success : R.drawable.fail); 

     // Setting OK Button 
     alertDialog.setButton("OK", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int which) { 
      } 
     }); 

     // Showing Alert Message 
     alertDialog.show(); 
    } 
} 
+0

你的意思是我必須爲alertDialog創建一個類,而不是簡單地在if語句的主體中調用該類?我不知道我的代碼,我正確的方式? – saman

+0

您不僅必須在您的主體中聲明「public void showSettingsAlert()」,還要創建新類以顯示警報對話框。 – BBonDoo