아;;;;보름넘게 해결을 못하고 있네요 ㅠㅠㅠㅠㅠㅠㅠㅠ
리스트 액티비티로 수신함전체를 불러왔는데요
추가적으로 버튼을이용하여 여지껏받았던 메시지를 삭제하고싶은데
삭제 하는부분이 자꾸 꼬여서 안되네요 ㅠㅠㅠㅠ
코딩상에 에러는 없는듯 보이는데 컴파일하게되면 에뮬레이터 상에서 에러가 뜨는데 원인을 모르겠네요 ㅠㅠㅠ
고수님 도와주세요 ㅠㅠㅠㅠㅠ
package pro.android;
import java.util.ArrayList;
import android.app.ListActivity;
import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Toast;
public class UnNagActivity extends ListActivity
{
//private ListAdapter adapter;
ArrayList<String> list;
ArrayAdapter<String> adapter;
private Button delete;
private static final Uri SMS = Uri.parse("content://sms/inbox");
@Override
public void onCreate(Bundle buldle)
{
super.onCreate(buldle);
setContentView(R.layout.main);
Cursor c = getContentResolver().query(SMS, null, null, null, null);
startManagingCursor(c);
String[] columns = new String[] {"body"};
//String [] names = new String [] {R.id.row};
list = new ArrayList<String>();
delete = (Button)findViewById(R.id.delete_button); // 버튼 생성
delete.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View v)
{
deleteNags();
}
private void deleteNags()
{
ContentResolver cr = getContentResolver();
Uri SMS = Uri.parse( "content://sms/inbox" );
Cursor cursor = cr.query(SMS, new String[] { "_id", "thread_id", "body" }, null, null, null);
if (cursor == null)
return;
if (!cursor.moveToFirst())
return;
int count = 0;
do
{
String body = cursor.getString( 2 );
if( body.indexOf( "FRM:nagios@" ) == -1 )
continue;
long thread_id = cursor.getLong( 1 );
Uri thread = Uri.parse( "content://sms/conversations/" + thread_id );
cr.delete( thread, null, null );
count++;
}
//getContentResolver().delete(Uri.parse("content://sms/conversations/" + thread_id),null,null);
while ( cursor.moveToNext() );
message( "Deleted: " + count );
}
private void message(String msg ) // 마지막 출력부분
{
Context context = getApplicationContext();
CharSequence text = msg;
Toast toast = Toast.makeText( context, text, Toast.LENGTH_SHORT );
toast.show();
};
});
adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, list);
setListAdapter(adapter);
adapter.notifyDataSetChanged();
}
}
sms 수신함을 불러오는 문제는 해결했으나 버튼을 생성하고서 삭제에대한 query부분이 적용이 안되는 문제가 있네요
("content://sms/inbox/1")이런식으로 삭제를 하면 된다고 하는데 방법을 전혀 모르겠습니다ㅠ
ㅜㅜㅜㅜ 정말 답답하네요 ㅠㅠㅠㅠ
자세히 보니;;; ListActivity를 상속받는 클래스인데 setContentView가 들어가 있네요;
android.R.layout.simple_list_item_1.xml파일엔 버튼이 없는데.. 직접 만드신건지는 잘 모르겠지만
만약 삭제 버튼이 main.xml에 등록되어 있고, 이를 findViewById로 받아 쓰려 한거라면 삭제 버튼을 눌렀을때 vm이 떡실신될만 하네요;;
로그캣에서 무슨 에러메시지가 나오던가요?
저도 sms 송.수신쪽으로 어플을 개발하고 있는 입장이라; 도와드리고 싶네요 ㅎ
이 쿼리를 날리셨다면 SMS table에서 해당 thread_id로 되어 있는 데이터가 모두 삭제 됩니다.
아니면 특전 SMS만 삭제 하고 싶으신거면 Uri를 "content://sms/1" <- 여기서 1은 님소스의 _id값
로 하시면 됩니다.
참고로 님이 댓글에 말씀하신 "content://sms/inbox/1"로 삭제하는 로직은 Android Default에서 지원하지 않습니다. 로그켓을 잘 살펴 보시면 "Unknown URL" 이라는 에러가 발생 했을 겁니다.
혹시 안드로이드 매니패스트 xml에서 sms접근 및 삭제 관련 권한 주셨는지요?.