主要活动代码。 BroadcastExample.java
package com.example.broadcast;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
public class BroadcastExaple extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d("PHONE", "Main Activity...Called");
setContentView(R.layout.main);
Log.d("PHONE", "After Mainf Activity...");
}
}
MyBroadCastReceiver.java
package com.example.broadcast;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
/*public void onReceive(Context context, Intent intent) {
try{
Log.d("Call BroadCAST","Calling Broad CAST");
MyPhoneStateListener phoneListener = new MyPhoneStateListener();
TelephonyManager telephony = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
telephony.listen(phoneListener, PhoneStateListener.LISTEN_CALL_STATE);
}
catch(Exception e)
{
}
}*/
public void onReceive(Context context, Intent intent) {
Log.d("Message", "Message Received");
}
}
的AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.broadcast" android:versionCode="1"
android:versionName="1.0">
<uses-prmission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.CALL_PHONE"/>
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".BroadcastExaple" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".MyBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
</application>
<uses-sdk android:minSdkVersion="7" />
</manifest>
程序不会进入MyBroadCastReceiver类的onReceive Mathod。
答案 0 :(得分:1)
首先,我建议您在此处格式化代码。这样人们就会很高兴阅读您的问题代码。 上面的代码列表不会将任何BroadcastReceiver注册到系统。您最好查看ApiDemo了解更多详情。