1
在這裏,我想讀它,我所看到的各種實例的消息和烤麪包片,其中有一個擴展BroadcastReceiver
單獨的一類,但他們都沒有提到如何啓動這個類(我們使用startactivity()或財產以後其他)。我發佈了通過O'Reilly烹飪書鏈接的代碼。我試圖從ddms短信,但它不顯示消息的祝酒詞。任何幫助表示讚賞,因爲這是我第一次使用BroadcastReceiver。開始廣播接收器
invitationSMSreciever.java
package com.SMS;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.sax.StartElementListener;
import android.telephony.SmsMessage;
import android.util.Log;
import android.widget.Toast;
public class invitationSMSreciever extends BroadcastReceiver {
final String TAG = "BombDefusalApp";
@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String message = "";
if (bundle != null) {
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for (int i = 0; i < msgs.length; i++) {
msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
message = msgs[i].getMessageBody();
Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
if (msgs[i].getMessageBody().equalsIgnoreCase("Invite")) {
// Intent myIntent = new Intent(MainMenu.this,
// com.bombdefusal.ReceivedSMSActivity.class);
Intent myIntent = new Intent();
myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
myIntent.setAction("com.example.helloandroid.INVITE");
context.startActivity(myIntent);
}
}
}
}
}
的MainMenu
package com.SMS;
import com.SMS.R;
import android.app.Activity;
import android.os.Bundle;
public class MainMenu extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
ReceivedSMSActivity
package com.SMS;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import com.SMS.R;
public class ReceivedSMSActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
startService(new Intent ("com.android.PLAY"));
setContentView(R.layout.invite);
}
public boolean onKeyDown(int keyCode, KeyEvent service) {
stopService(new Intent("com.bombdefusal.START_AUDIO_SERVICE"));
finish();
return true;
}
}
清單
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.SMS" android:versionCode="1" android:versionName="1.0">
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".MainMenu" android:label="@string/app_name">
<intent-filter>
<action android:name="com.SMS" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.SMS.ReceivedSMSActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="com.example.helloandroid.INVITE"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
<receiver android:name="com.SMS.invitationSMSreciever"
android:enabled="true">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</receiver>
</application>
</manifest>
thax快速回復忘了不好意思張貼明顯 – dreamer1989
@Yuri IVE的附加清單文件,你可以看到一個接收器註冊清單它仍然不能正常使用PLZ你能解釋或回答如何initaite的廣播接收器類 – dreamer1989
Android系統的代碼調用你的廣播接收器,當它獲得對應於意圖過濾器,您在定義的意圖的方法的onReceive AndroidManifest.xml中。你是否收到模擬器的默認信使應用程序從ddms發送的短信?如果沒有,那麼問題不在你的代碼中。 – Yury