gpt4 book ai didi

java - Android获取BroadcastReceiver的方法

转载 作者:行者123 更新时间:2023-11-29 20:26:54 25 4
gpt4 key购买 nike

如何将 BroadcastReceiver 中的方法放入 Fragment 中?甚至有可能吗?接收者:

public class Receiver extends BroadcastReceiver {


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

}

fragment :

public class Test extends Fragment {
...
}

于是出现错误:

Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference at

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

在 fragment 中:

    receiver.sendNotificationIfTimeEnd01(context);
broadcastIntent();

编辑升级代码: fragment :

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

我的接收者:

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

}

最佳答案

实际上,您可以使用 Intents :

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

并声明您的广播接收器可以通过 list 对这种 Intent 使用react:

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

然后你可以在接收器中拦截这个Intent:

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

}
}

关于java - Android获取BroadcastReceiver的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32424052/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com