Look what Im doing. Im getting a SMS message and breaking word by word using the StringTokenizer class.I've done this, but when I try to insert the word in the content provider, the application stops, and the emulator says the application stoped unexpectadly. I don't know what to do anymore, I've tryied everything you can think about, but i didn't found the answer. =(
Please Help Me! Check my two classes.
SMS.java Class:
import java.util.StringTokenizer;
import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.BaseColumns;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class SMS extends Activity {
Button btnVerSms;
TextView txtView;
Integer i;
ContentValues value;
String palavra;
public SMSProvider provider = new SMSProvider();
public ContentValues cv = new ContentValues();
final Uri CONTENT_URI = Uri.parse("content://sms/sent");
public static final String AUTHORITY = "com.androiding.provider.SMS";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnVerSms= (Button)findViewById(R.id.btnVerSms);
txtView = (TextView)findViewById(R.id.txtView);
btnVerSms.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Cursor cursor = getContentResolver().query(CONTENT_URI, null, null, null, null);
String body = null;
if(cursor.moveToFirst()){
body = cursor.getString(cursor.getColumnIndexOrThrow("body")).toString();
//txtView.append(body);
StringTokenizer st = new StringTokenizer(body);
while(st.hasMoreTokens()){
cv.put(st.nextToken(), 1);
provider.insert(CONTENT_URI, cv);
//getBaseContext().getContentResolver().notify();
Toast.makeText(getBaseContext(), "inseriu", Toast.LENGTH_LONG).show();
}
}
}
});
}
}
SMSProvider.java
import java.util.HashMap;
import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.SQLException;
import android.net.Uri;
import android.util.Log;
import android.widget.Toast;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class SMSProvider extends ContentProvider{
public static final String PROVIDER_NAME = "br.com.androiding.provider.Sms";
public static final Uri CONTENT_URI = Uri.parse("content://"+PROVIDER_NAME+"/sms");
public static final String _ID = "_id";
public static final String WORD = "word";
public static final int COUNT = 0;
private static final int WORDS = 1;
private static final int WORDS_ID = 2;
private static final UriMatcher uriMatcher = null;
static{
uriMatcher.addURI(PROVIDER_NAME, "sms", WORDS);
uriMatcher.addURI(PROVIDER_NAME, "sms/#", WORDS_ID);
}
//for database use
private SQLiteDatabase smsDB;
private static final String DATABASE_NAME = "SmsWords";
private static final String DATABASE_TABLE = "words";
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_CREATE =
"create table" + DATABASE_TABLE +
"(_id integer primary key autoincrement," +
"word text not null, count integer not null);";
private static class DatabaseHelper extends SQLiteOpenHelper{
DatabaseHelper(Context context){
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public void onCreate(SQLiteDatabase db){//this method create the database
db.execSQL(DATABASE_CREATE);
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {//this method upgrades the database
Log.w("Content Provider Database", "Uprading database from version "+oldVersion+" to "+newVersion+", and all the old data will be destroyed.");
db.execSQL("DROP TABLE if exists words");
onCreate(db);
}
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
// TODO Auto-generated method stub
return 0;
}
@Override
public String getType(Uri uri) {
switch(uriMatcher.match(uri)){
case WORDS:
return "vnd.android.cursor.dir/vnd.androiding.sms ";
case WORDS_ID:
return "vnd.android.cursor.item/vnd.androiding.sms ";
default:
throw new IllegalArgumentException("Unsupported URI: " + uri);
}
}
@Override
public Uri insert(Uri uri, ContentValues values) {
long rowId = smsDB.insert(DATABASE_TABLE, "", values);
if(rowId>0){
Uri _uri = ContentUris.withAppendedId(CONTENT_URI, rowId);
getContext().getContentResolver().notifyChange(_uri, null);
return _uri;
}
throw new SQLException("Failed to insert row into "+uri);
}
@Override
public boolean onCreate() {
// TODO Auto-generated method stub
return false;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
// TODO Auto-generated method stub
return null;
}
@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
// TODO Auto-generated method stub
return 0;
}
public boolean onCreate(SQLiteDatabase arg0) {
Context context = getContext();
DatabaseHelper dbHelper = new DatabaseHelper(context);
smsDB = dbHelper.getWritableDatabase();
return (smsDB == null) ? false:true;
}
}