2016-06-03 7 views
0

Ich kann RegisterReceiver nicht in BroadcastReceiver verwenden. Ich lese, dass ich das nicht tun kann dann, wie soll ich bestätigen, dass meine SMS gesendet wurde?Kann RegisterReciever in BroadcastReceiver nicht verwenden

BroadCastReciever

public class MyReceiver extends BroadcastReceiver { 
    String SENT="sent"; 
    String DELIVERED="delivered"; 
    public final String tag="com.example.pritesh.smstimer"; 
    public MyReceiver() { 

    } 

    //@Override 
    public void onReceive(Context context, Intent intent) { 

      Log.i(tag,"Sending"); 
      final NotificationManager notificationManager= (NotificationManager) 
        context.getSystemService(Context.NOTIFICATION_SERVICE); 

      Intent intent1=new Intent(context,Time_Picker.class); 

      PendingIntent pendingIntent = PendingIntent.getActivity(context,0,intent1,0); 
     //Notification for sent msg 
     final Notification.Builder notification_sent=new Notification.Builder(context) 
        .setContentTitle("SMS SENT") 
        .setContentText("Your SMS has Been Sent") 
        .setContentIntent(pendingIntent) 
        .setSmallIcon(R.drawable.favicon); 
     //Notification for msg not sent 
     final Notification.Builder notification_notsent=new Notification.Builder(context) 
       .setContentTitle("SMS SENT") 
       .setContentText("Your SMS was not sent due to no network service") 
       .setContentIntent(pendingIntent) 
       .setSmallIcon(R.drawable.favicon); 
     //Intent for sending confirmation 

     Intent sent=new Intent(SENT); 
     //PendingIntent for sending confirmation 

     PendingIntent sentIntent=PendingIntent.getBroadcast(context,0,sent,PendingIntent.FLAG_UPDATE_CURRENT); 
     //Reciever for sent confirmation 

     Intent deliveryIntent = new Intent(DELIVERED); 

     PendingIntent deliverPI = PendingIntent.getBroadcast(
       context, 0, deliveryIntent, 
       PendingIntent.FLAG_UPDATE_CURRENT); 

     context.registerReceiver(new BroadcastReceiver() { 
      @Override 
      public void onReceive(Context context, Intent intent) { 
       String result = ""; 

       switch (getResultCode()) { 

        case Activity.RESULT_OK: 
         result = "Transmission successful"; 
         notificationManager.notify(0,notification_sent.build()); 
         break; 
        case SmsManager.RESULT_ERROR_NO_SERVICE: 
         result = "No service"; 
         notificationManager.notify(0,notification_notsent.build()); 
         break; 
       } 
      } 
     },new IntentFilter(SENT)); 

     context.registerReceiver(new BroadcastReceiver() { 

      @Override 
      public void onReceive(Context context, Intent intent) { 
       Toast.makeText(context, "Deliverd", 
         Toast.LENGTH_LONG).show(); 
      } 

     }, new IntentFilter(DELIVERED)); 


      SmsManager smsManager = SmsManager.getDefault(); 
      smsManager.sendTextMessage(Time_Picker.Phone,null, Time_Picker.Message, sentIntent 
        ,deliverPI); 

      Toast.makeText(context, "Sms Sent", Toast.LENGTH_LONG).show(); 

    } 

} 

Wie soll ich das sentIntent senden, um zu bestätigen, ob SMS gesendet oder nicht

Manifest

<?xml version="1.0" encoding="utf-8"?> 

<uses-permission android:name="android.permission.SEND_SMS" /> 

<application 
    android:allowBackup="true" 
    android:icon="@drawable/favicon" 
    android:label="@string/app_name" 
    android:supportsRtl="true" 
    android:theme="@style/AppTheme" > 
    <activity 
     android:name=".Time_Picker" 
     android:theme="@style/AppTheme.NoActionBar" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 

    <receiver 
     android:name=".MyReceiver" 
     android:enabled="true" 
     android:excludeFromRecents="true" 
     android:exported="true" 
     android:launchMode="singleTask" 
     android:taskAffinity="" /> 
    <receiver 
     android:name=".NotificationReciever" 
     android:enabled="true" 
     android:exported="true" > 
    </receiver> 
</application> 

Antwort

0

Sie können dynamische BroadcastReceiver s nicht in einem anderen BroadcastReceiver registrieren. Dies liegt daran, dass das System erzwingt, dass ein BroadcastReceiver seine Arbeit innerhalb einer kurzen Zeitspanne beendet (ich denke, es ist entweder 5 oder 10 Sekunden).

Sie können ein weiteres BroadcastReceiver erstellen, das in Ihrem AndroidManifest registriert ist. Die <intent-filter> sollte <action> Elemente enthalten, die den benutzerdefinierten Aktionszeichenfolgen entsprechen, die Sie in Ihrem Code verwenden.

+0

Kannst du mir den Manifestteil für die neue Absicht zeigen – Pritesh

+0

Danke, das hat für mich funktioniert – Pritesh