これは私のマニフェストファイルです
<?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);
}
}