0

当收到消息时,我很难以我想要的地图形式接收短信 alertdialog ok 并在地图出现时取消 click ok help me

我的代码

//短信接收器.java

public class SMSReceiver extends BroadcastReceiver
{
    static final String ACTION = "android.provider.Telephony.SMS_RECEIVED";

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

        //---get the SMS message passed in---
        Bundle bundle = intent.getExtras();        
        SmsMessage[] msgs = null;
        String str = "";            
        if (bundle != null)
        {
            //---retrieve the SMS message received---
            Object[] pdus = (Object[]) bundle.get("pdus");
            msgs = new SmsMessage[pdus.length];            
            for (int i=0; i<msgs.length; i++){
                msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);                
                str += "SMS from " + msgs[i].getOriginatingAddress();                     
                str += " :";
                str += "Location  " + " Longitude : " + lon  + "\n Latitude : "  + lat;
                str += msgs[i].getMessageBody().toString();
                str += "\n";        
            }

               String action = intent.getAction();
        if (ACTION.equals(action)) 
        {
            //Start Activity here
             Intent startIntent = new Intent(context, SMSNotif.class);
                     startIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                     startIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
                     String name = "pesan";
                     String value = "str";
                     startIntent.putExtra("pesan",String.valueOf(str));
                     context.startActivity(startIntent);

        }    
    }
} 
}

//messageNotif.java

public class messageNotif extends Activity 
{
private static final String LOG_TAG = "SMSReceiver";
public static final int NOTIFICATION_ID_RECEIVED = 0x1221;
public Intent i;
private String pesan ;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
     setContentView(R.layout.main);
     Bundle extras = getIntent().getExtras();
     pesan = String.valueOf(extras.getString("pesan"));

     //---display the new SMS message---
     Toast.makeText(null,"budug"+pesan, Toast.LENGTH_LONG).show();

     // call displayAlert() here
        displayAlert();
}
    public void displayAlert()
    {
        AlertDialog.Builder builder = new AlertDialog.Builder(this).setTitle("Rehere");
        builder.setMessage("Are you open in rehere?").setCancelable(
            false).setPositiveButton("Ok",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                launchIntent();

                }

                private void launchIntent() {
                    Intent myIntent = new Intent(getBaseContext(), My_Map.class);             
                    startActivity(myIntent);
                }
            }).setNegativeButton("Cancel",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
                }
            });
        AlertDialog alert = builder.create();
        alert.show();
     }

}
}

//map.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/myLocationText"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

    <com.google.android.maps.MapView
        android:id="@+id/map"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:apiKey="0d8Ntrj9uYka52jVwlG4TacE1HwZB6YSxFF_YZA"
        android:clickable="true"
        android:enabled="true" />

</LinearLayout>

我的代码有什么问题??请你帮助我好吗

4

1 回答 1

0

1.Intent startIntent = new Intent(context, SMSNotif.class); 但是公共类 messageNotif 扩展了 Activity

2.Toast.makeText(null,"budug"+pesan, Toast.LENGTH_LONG).show(); 第一个参数应该像 Toast.makeText(this,.....

3.startIntent.putExtra("pesan",String.valueOf(str));

捆绑附加服务 = getIntent().getExtras(); pesan = String.valueOf(extras.getString("pesan")); 这里的 Bundle extras 为空

pesan = getIntent().getStringExtra("pesan")

4.launchIntent();应该在一个类中声明

你可以只替换方法的代码

于 2013-05-30T07:21:35.180 回答