0

生成通知這是我的清單文件無法從廣播接收器

<?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> 

與之相似,這是我的廣播接收器類。並且整個應用程序運行,但是當電話響鈴或任何電話狀態改變時,我都不會收到任何通知。因此,任何人都可以幫我.the援助將不勝感激

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); 

    } 
} 

回答

1

也許檢查代碼intent.putExtra();它應該在sendBroadCast()或PendingIntent.getBroadCast()之前。 我也有類似的問題。最後我發現我在getBroadCast()之後執行putExtra(),所以意圖實際上並不包含任何內容。