这是我的清单文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.broadcastlistner"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="9" />
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.broadcastlistner.MainActivity"
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="com.example.broadcastlistner.receiver.MyBroadCastListener"
android:enabled="true"
android:exported="false">
<intent-filter>
<action android:name="android.intent.com.example.broadcastlistner.receiver.MyBroadCastListener.PHONE_STATE" />
<category android:name="android.intent.category.Default" />
</intent-filter>
</receiver>
</application>
</manifest>
同样,这是我的广播接收器类。并且整个应用程序运行,但是当电话响铃或任何电话状态正在改变时,我没有收到任何通知。因此任何人都可以帮助我。我们将不胜感激
package com.example.broadcastlistner;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.telephony.*;
import android.util.Log;
public class MyBroadCastListener extends BroadcastReceiver{
@Override
public void onReceive(final Context context, Intent intent) {
String phoneNumber = null;
Bundle extras = intent.getExtras();
if (intent.getExtras() != null) {
String state = extras.getString(TelephonyManager.EXTRA_STATE);
if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
phoneNumber = extras
.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
if(phoneNumber == null || phoneNumber.trim().length()==0)
phoneNumber = "";
Log.i(state, "Incomming no");
createNotification("Incomming Number:",phoneNumber,context);
}
else if(state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK))
{
System.out.println("call Disconnected");
Log.i(state, "offhook");
createNotification("Phone Disconnected:",phoneNumber,context);
}
else if(state.equals(TelephonyManager.EXTRA_STATE_IDLE))
{
System.out.println("Phone is idle");
Log.i(state, "phone idle");
createNotification("Phone Idle:",phoneNumber,context);
}
}
// check the current state
}
public static void createNotification(String subject,String phoneNumber,Context context)
{
// Prepare intent which is triggered if the
// notification is selected
Intent intent = new Intent(context, MainActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(context, 0, intent, 0);
// Build notification
// Actions are just fake
String longText="this is a test message and will be handled after ward";
Notification noti = new NotificationCompat.Builder(context)
.setContentTitle(subject+" "+ "From: " )
.setContentText(phoneNumber)
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(pIntent)
//.setSound(sound)
.setStyle(new NotificationCompat.BigTextStyle().bigText(longText))
.addAction(R.drawable.ic_launcher, "Call", pIntent)
.addAction(R.drawable.ic_launcher, "More", pIntent)
.addAction(R.drawable.ic_launcher, "And more", pIntent).build();
NotificationManager notificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
// Hide the notification after its selected
noti.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, noti);
}
}