2015-09-06 65 views
1

如何將我的BroadcastReceiver中的方法放入我的片段? 或者它甚至有可能嗎? 接收機:Android獲取BroadcastReceiver的方法

public class Receiver extends BroadcastReceiver { 


    public void test(Context c) { 
    .... 
    } 

} 

片段:

public class Test extends Fragment { 
... 
} 

因此,一個錯誤出現了:

嘗試調用虛擬方法「android.content.Context android.content.Context.getApplicationContext( )'在空對象上 參考 在

com.test.tab.MyReceiver.sendNotificationIfTimeEnd01(MyReceiver.java:47) 

     at com.test.tab.Juli.broadcastIntent(Juli.java:54) 

     at com.test.tab.Juli.onCreateView(Juli.java:188) 

對於這些線路中的代碼是: 在接收器:

public void sendNotificationIfTimeEnd01(Context c) { 
     Context context = c.getApplicationContext(); 
     Intent intent01 = new Intent(context, MainActivity.class); 
.... 
} 

和在片段:

receiver.sendNotificationIfTimeEnd01(context); 
    broadcastIntent(); 

EDIT UPGRADED CODE: 片段:

public class FragmentTest extends Fragment { 

    private Context context; 


    IntentFilter filter = new IntentFilter("com.example.Broadcast"); 
MyReceiver receiver = new MyReceiver(); 

// Call this method when the condition is met. 
public void broadcastIntent() { 
    Intent intent = new Intent(); 
    intent.setAction("com.example.Broadcast"); 
    getActivity().sendBroadcast(intent); 
} 

...

if (diffDays <= 0 && diffHours <= 0 && diffMinutes <= 0) { 
     ((TextView) android.findViewById(R.id.test)).setText("test works"); 
     if (!notification043 && !buttonColor01.equals("red01")) { 
      broadcastIntent(); 
      editor.putBoolean("notification43", true); 
      editor.apply(); 
     } 
    } 

}

MyReceiver:

public class MyReceiver extends BroadcastReceiver { 
    public static final int NOTIFICATION_ID_01 = 1; 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     sendNotificationIfTimeEnd01(context); 
    } 

    public void sendNotificationIfTimeEnd01(Context c) { 
     Context context = c.getApplicationContext(); 
     Intent intent01 = new Intent(context, MainActivity.class); 
     PendingIntent pendingIntent01 = PendingIntent.getActivity(context, 1, intent01, 0); 
     NotificationCompat.Builder builder = 
       new NotificationCompat.Builder(context) 
         .setSmallIcon(R.drawable.ic_stat_notification) 
         .setContentIntent(pendingIntent01) 
         .setAutoCancel(true) 
         .setLargeIcon(BitmapFactory.decodeResource(c.getResources(), R.drawable.ic_launcher)) 
         .setContentTitle(testArray[0]) 
         .setContentText("testready") 
         .setSubText("click here"); 
     NotificationManager notificationManager = 
       (NotificationManager) c.getSystemService(c.NOTIFICATION_SERVICE); 
     notificationManager.notify(NOTIFICATION_ID_01, builder.build()); 
     try { 
      Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
      Ringtone r = RingtoneManager.getRingtone(c.getApplicationContext(), notification); 
      r.play(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

} 
+0

我想這是不可能的,因爲'BroadcastReceiver's由操作系統本身實例化。你想做什麼,爲什麼你需要這種方法? –

+0

我想在我的廣播接收器中有一個方法發生,如果發生在我的片段內的情況 –

+0

條件是什麼?這是一個事件,你已經註冊了你的'BroadcastReceiver'嗎? –

回答

2

其實,你可以使用意圖:

public class Test extends Fragment { 
    ... 
    // Call this method when the condition is met. 
    public void broadcastIntent() { 
     Intent intent = new Intent(); 
     intent.setAction("com.example.Broadcast"); 
     getActivity().sendBroadcast(intent); 
    } 
} 

,並宣佈你的廣播接收器能夠反應到這種Inten牛逼或者通過清單:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.BroadcastDetector" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk android:minSdkVersion="10" /> 

    <application 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" > 
     <receiver android:name="MyReceiver" > 
      <intent-filter> 
       <action android:name="com.example.Broadcast" > 
        </action> 
      </intent-filter> 
     </receiver> 
    </application> 
</manifest> 

或編程

IntentFilter filter = new IntentFilter("com.example.Broadcast"); 

MyReceiver receiver = new MyReceiver(); 
getActivity().registerReceiver(receiver, filter); 

然後你就可以在接收器截獲此意圖:

public class MyReceiver extends BroadcastReceiver { 
    @Override 
    public void onReceive(Context arg0, Intent arg1) { 
     // TODO Auto-generated method stub 

    } 
} 
+0

好吧我試試這個謝謝 –

+0

你的意思是registerReceiver裏面我的廣播接收器的方法?我無法解決它。 –

+0

沒有片段寫?仍然不行=) –