0
내 브로드 캐스트받는 사람이 wifi 상태가 변경되면 해당 MainActivity에서 메서드를 호출하려고합니다. 내 수신기 클래스 내 코드는 다음과 같습니다 다음 브로드 캐스트 리시버에 대한broadcastreceiver 다른 클래스에서 메서드를 호출하십시오.
public class ActionReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Wifi toggled", Toast.LENGTH_SHORT).show();
}
(토스트는 완벽하게 잘 작동) 그리고 난 다음과 같습니다 통지 생성하는 메서드를 호출 할 :
public void createNotification() {
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(getResources().getString(R.string.nTitle))
.setContentText(getResources().getString(R.string.nText));
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(contentIntent);
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(notiId, builder.build());
}
경우를 그냥 말하면 :
MainActivity.createNotification();
onReceive 작동하지 않습니다. 내가 도대체 뭘 잘못하고있는 겁니까?
도움 주셔서 감사합니다.
당신은 할 수 모든 코드를 onReceive 메소드로 옮기거나 메소드를 정적으로 만들고 컨텍스트를 전달하십시오. 그 중 하나가 작동해야합니다. 하지만 MainActivity.createNotification() 메소드가 정적이 아니기 때문에 MainActivity.createNotification()이 작동하지 않을 것입니다. – cjbrooks12