My app reads the messages from the phone's inbox and displays it on a list view in the app. I am trying to extract the message's content itself and store it as a string for future intent use. How do i go about this? When i check log cat it tells me that my content in the list view is not even a string in the first place so i cant extract it just yet. Do i need to convert the content to string first? if yes, how can i do this?
This is my current codes, how i extracted and displayed the messages from the inbox to the app:
public class MainActivity extends AppCompatActivity implements OnClickListener {
// GUI Widget
Button getSmsButton, viewGoogleMapButton;
TextView lblMsg, lblNumber;
ListView messageListView;
Context ctx = this;
String msg;
// Cursor Adapter
SimpleCursorAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Init GUI Widget
getSmsButton = (Button) findViewById(R.id.getSmsButton);
getSmsButton.setOnClickListener(this);
messageListView = (ListView) findViewById(R.id.messageListView);
viewGoogleMapButton = (Button) findViewById(R.id.viewGoogleMapButton);
viewGoogleMapButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
Intent startGoogleMap = new Intent(ctx, MapsActivity.class);
startActivity(startGoogleMap);
}
});
}
@Override
public void onClick(View v) {
if (v != getSmsButton)
return;
// Create Inbox box URI
Uri inboxURI = Uri.parse("content://sms/inbox");
// List required columns
String[] reqCols = new String[]{"_id", "address", "body"};
// Get Content Resolver object, which will deal with Content Provider
ContentResolver cr = getContentResolver();
// Fetch Inbox SMS Message from Built-in Content Provider
Cursor c = cr.query(inboxURI, reqCols, null, null, null);
// Attached Cursor with adapter and display in listview
adapter = new SimpleCursorAdapter(this, R.layout.row, c,
new String[]{"body", "address"}, new int[]{
R.id.lblMsg, R.id.lblNumber});
messageListView.setAdapter(adapter);
}
}
I tried putting these codes to extract the content and store as string but it does not work
messageListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String data=(String)parent.getItemAtPosition(position);
Log.i("msg", data);
}});
}
this is my row.xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/lblMsg"></TextView>
<TextView
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#00f"
android:id="@+id/lblNumber"></TextView>