我创建了一个应用程序,我在其中检索从MainActivity到SmsSend的联系人。在活动结果方法上,但它没有回馈结果。所以那个联系人用户可以发送消息。
SmsSend.java
package com.example.smscampaign;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
public class SmsSend extends Activity implements OnClickListener {
BroadcastReceiver smsSentReciver, smsSentDelivery;
static EditText edName, edMessage;
static int ResultCode = 12;
static ArrayList<String> sendlist = new ArrayList<String>();
Button btnSaveForLater, btnStart, btnExternal, btnSelect;
static TextView txt;
static StringBuilder conct = new StringBuilder();
static String contacts = "";
String delim = ";";
public static String Name;
int i = 0;
String Name1, msg, msg1;
String[] cellArray;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.create_camp);
ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
edName.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
edName.setError(null);
}
});
edMessage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
edMessage.setError(null);
}
});
txt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
txt.setError(null);
}
});
// Toast.makeText(getApplication(), contacts.toString(),
// Toast.LENGTH_LONG).show();
Name1 = edName.getText().toString();
msg1 = edMessage.getText().toString();
}
@Override
public void onBackPressed() {
// TODO Auto-generated method stub
super.onBackPressed();
edName.setText(null);
edMessage.setText(null);
conct.delete(0, conct.length());
contacts = null;
txt.setText("0");
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
unregisterReceiver(smsSentReciver);
unregisterReceiver(smsSentDelivery);
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
smsSentReciver = new BroadcastReceiver() {
@Override
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
switch (getResultCode()) {
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "sms has been sent",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Toast.makeText(getBaseContext(), "Generic Fail",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
Toast.makeText(getBaseContext(), "No Service",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
Toast.makeText(getBaseContext(), "Null PDU",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
Toast.makeText(getBaseContext(), "Radio Off",
Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}
};
smsSentDelivery = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
switch (getResultCode()) {
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "Sms Delivered",
Toast.LENGTH_SHORT).show();
break;
case Activity.RESULT_CANCELED:
Toast.makeText(getBaseContext(), "Sms not Delivered",
Toast.LENGTH_SHORT).show();
break;
}
}
};
registerReceiver(smsSentReciver, new IntentFilter("SMS_SENT"));
registerReceiver(smsSentDelivery, new IntentFilter("SMS_DELIVERED"));
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
DatabaseHelp entry = new DatabaseHelp(SmsSend.this);
entry.open();
switch (v.getId()) {
case R.id.btnSelect:
Intent a = new Intent(SmsSend.this, MainActivity.class);
a.putExtra("rowid", rowid);
startActivity(a);
break;
case R.id.btnExternal:
Intent file = new Intent(SmsSend.this, File_Selecter.class);
startActivity(file);
break;
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == ResultCode) {
if (resultCode == RESULT_OK) {
sendlist = data.getStringArrayListExtra("name");
if (sendlist != null) {
for (int i = 0; i < sendlist.size(); i++) {
conct.append(sendlist.get(i).toString());
conct.append(delim);
//Toast.makeText(getApplication(), conct.toString(),
// Toast.LENGTH_LONG).show();
}
}
}
i = sendlist.size();
txt.setText(Integer.toString(i));
if (resultCode == RESULT_CANCELED) {
}
}
}
}
MainaActivity.java
package com.example.smscampaign;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.widget.AdapterView.OnItemClickListener;
public class MainActivity extends Activity implements OnItemClickListener {
ArrayList<String> name1 = new ArrayList<String>();
static ArrayList<String> phno1 = new ArrayList<String>();
ArrayList<String> phno0 = new ArrayList<String>();
MyAdapter ma;
Button send;
String[] cellArray = null;
int[] str;
int v = 0;
String contacts;
static int check1;
ListView lv;
int index;
int top;
StringBuilder b = new StringBuilder();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
setContentView(R.layout.get);
TextView txt = (TextView) findViewById(R.id.textView1);
getAllCallLogs(this.getContentResolver());
lv = (ListView) findViewById(R.id.lv);
ma = new MyAdapter();
lv.setAdapter(ma);
lv.setOnItemClickListener(this);
lv.setItemsCanFocus(false);
lv.setTextFilterEnabled(true);
// b = SmsSend.contacts;
contacts = SmsSend.contacts;
//if (SmsSend.contacts != null) {
cellArray = contacts.split(";");
// Toast.makeText(getApplication(),contacts.toString(),
// Toast.LENGTH_LONG).show();
for (int i = 0; i < cellArray.length; i++) {
for (int j = 0; j < phno1.size(); j++) {
if (cellArray[i].equals(phno1.get(j))) {
Toast.makeText(getApplication(),cellArray[i].toString(),
Toast.LENGTH_LONG).show();
ma.setChecked(j, true);
}
}
}
}
//}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
getMenuInflater().inflate(R.menu.contact_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.addPage:
break;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onBackPressed() {
// TODO Auto-generated method stub
StringBuilder checkedcontacts = new StringBuilder();
System.out.println(".............." + ma.mCheckStates.size());
for (int i = 0; i < name1.size(); i++)
{
if (ma.mCheckStates.get(i) == true) {
phno0.add(phno1.get(i).toString());
checkedcontacts.append(name1.get(i).toString());
checkedcontacts.append("\n");
} else {
System.out.println("..Not Checked......"
+ name1.get(i).toString());
}
}
//Toast.makeText(MainActivity.this, checkedcontacts, 1000).show();
Intent returnIntent = new Intent();
returnIntent.putStringArrayListExtra("name", phno0);
setResult(RESULT_OK, returnIntent);
finish();
}
}
答案 0 :(得分:3)
这是intent的一个示例函数..稍微修改一下
Bundle extras = getIntent().getExtras();
String strEmployeeID="";
if (extras != null)
{
String value = extras.getString("new_variable_name");
// Toast.makeText(getBaseContext(), value, Toast.LENGTH_LONG).show();
strEmployeeID = value;
}
Intent i = new Intent(getApplicationContext(), yourdesiredclass.class);
i.putExtra("new_variable_name",strEmployeeID);
startActivity(i);
}
答案 1 :(得分:1)
使用startActivityForResult()而不是startActivity()
答案 2 :(得分:1)
你应该使用startActivityForResult()开始活动;并将@Override
注释添加到onActivityResult
startActivityForResult(intent, requestCode);
答案 3 :(得分:0)
ref Below link:
http://developer.android.com/guide/components/services.html
-----------------------------------------------------------
startService(i1);//Start Service.
Use startActivityForResult() instead of startActivity() bcoz in service we use startActivityResult()
method bcoz broadcast receiver will give the response of onresultActivity.. so you could use this line
Use startActivityForResult(intent,requestcode) instead of startActivity().
thank you..