標題:
MySMSReceiver
[打印本頁]
作者:
ray
時間:
2012-11-28 18:59
標題:
MySMSReceiver
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Spinner
android:id="@+id/Spinner01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<ListView
android:id="@+id/ListView01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
複製代碼
作者:
ray
時間:
2012-11-28 19:06
package com.example.mysmsreceiver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class MySMSReceiver extends BroadcastReceiver {
public MySMSReceiver() {
}
@Override
public void onReceive(Context context, Intent intent) {
// TODO: This method is called when the BroadcastReceiver is receiving
// an Intent broadcast.
throw new UnsupportedOperationException("Not yet implemented");
}
}
複製代碼
作者:
ray
時間:
2012-11-28 19:10
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mysmsreceiver"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".MySMSReceiver">
<intent-filter android:priority="100">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
</manifest>
複製代碼
作者:
ray
時間:
2012-11-28 19:13
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
複製代碼
作者:
ray
時間:
2012-11-28 19:26
private static final String DATABASE_NAME = "SMSDB";
private static final int DATABASE_VERSION = 1;
複製代碼
作者:
ray
時間:
2012-11-28 19:44
try
{
String sql = "create table smstb(mobile text not null,message text not null)";
db.execSQL(sql);
}
catch(Exception e)
{
}
複製代碼
作者:
ray
時間:
2012-11-28 19:48
if(intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED"))
{
}
複製代碼
作者:
ray
時間:
2012-11-28 19:56
Object[] pdus = (Object[])intent.getExtras().get("pdus");
複製代碼
作者:
ray
時間:
2012-11-28 20:01
SmsMessage[] message = new SmsMessage[pdus.length];
複製代碼
作者:
ray
時間:
2012-11-28 20:09
for(int i=0;i<pdus.length;i++)
{
message[i]=SmsMessage.createFromPdu((byte[])pdus[i]);
String mobile = message[i].getDisplayOriginatingAddress();
String msg = message[i].getDisplayMessageBody();
}
複製代碼
作者:
ray
時間:
2012-11-28 20:14
SMSDB myDB = new SMSDB(context);
final SQLiteDatabase db = myDB.getWritableDatabase();
String sql = "insert into smstb values('"+mobile+"','"+msg+"');";
db.execSQL(sql);
複製代碼
作者:
ray
時間:
2012-11-28 20:18
String show = "";
show += "接收来自:" + mobile;
show += "的簡訊内容如下:\n"+msg;
Toast.makeText(context, show, Toast.LENGTH_LONG).show();
複製代碼
作者:
ray
時間:
2012-11-28 21:19
package com.example.mysmsreceiver;
import android.os.Bundle;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.AdapterView.OnItemSelectedListener;
public class MainActivity extends Activity {
private SMSDB helper;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
helper = new SMSDB(this);
final SQLiteDatabase db = helper.getReadableDatabase();
Cursor c = db.query ("smstb", new String[]{"mobile"}, null, null, "mobile", null, null);
c.moveToFirst();
CharSequence[] list = new CharSequence[c.getCount()];
for(int i = 0; i < list.length; i++)
{
list[i] = c.getString(0);
c.moveToNext();
}
c.close();
Spinner spinner = (Spinner)this.findViewById(R.id.Spinner01);
spinner.setAdapter(new ArrayAdapter<CharSequence>(this,android.R.layout.simple_spinner_item,list));
spinner.setOnItemSelectedListener(new OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
String mobile = ((Spinner)parent).getSelectedItem().toString();
Cursor c = db.query("smstb", new String[]{"message"}, "mobile='"+mobile+"'", null, null, null, null);
c.moveToFirst();
CharSequence[] list = new CharSequence[c.getCount()];
for(int i = 0; i < list.length; i++)
{
list[i] = c.getString(0);
c.moveToNext();
}
c.close();
ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(MainActivity.this,android.R.layout.simple_list_item_1,list);
ListView listView = (ListView)findViewById(R.id.ListView01);
listView.setAdapter(adapter);
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://seed.istak.org.tw/)
Powered by Discuz! 7.2