我很难以地图的形式接收短信 我想在收到alertdialog ok时收到消息,当地图出现时取消点击确定 帮助我
我的代码
// SMSReceiver.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>
我的代码出了什么问题? 你能帮我吗
答案 0 :(得分:0)
1.Intent startIntent = new Intent(context,SMSNotif.class); 但公共类messageNotif扩展了Activity
2.Toast.makeText(null,“budug”+ pesan,Toast.LENGTH_LONG)。show(); 第一个arg应该像Toast.makeText(this,.....
3.startIntent.putExtra( “pesan”,将String.valueOf(STR));
Bundle extras = getIntent()。getExtras(); pesan = String.valueOf(extras.getString(“pesan”)); 这里Bundle extras为null
pesan = getIntent()。getStringExtra(“pesan”)
4.launchIntent();应该在类
中声明你可以只替换方法的代码