gpt4 book ai didi

android - 在 Notification 的操作按钮上设置 onClickListener

转载 作者:行者123 更新时间:2023-11-29 19:27:09 24 4
gpt4 key购买 nike

我正在向用户发送一个通知,它有两个操作按钮“接受”和“拒绝”。

一旦用户点击“接受”,我想检查几个条件然后相应地进行。我不希望用户只导航到 Intent 中指定的屏幕。

更新:在执行以下代码时,通知已到达,但在单击“接受”或“拒绝”按钮时,没有任何反应!

这是我正在做的:

        public class MyService extends Service {

DatabaseReference firebaseDatabaseFollowers;
boolean newRequestBool;
NotificationCompat.Builder mBuilder;
String pBy, ss;
GeoFire geoFire;
GeoQuery geoQuery;
Query query;

@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}

@Override
public void onCreate() {
super.onCreate();

firebaseDatabase = FirebaseDatabase.getInstance().getReferenceFromUrl("https://***-***.firebaseio.com/");
geoFire = new GeoFire(FirebaseDatabase.getInstance().getReferenceFromUrl("https://***-***.firebaseio.com/geofire"));
query = firebaseDatabaseFollowers.child("users").child(MainActivity.uid).child("child");

final SharedPreferences sharedPref2 = PreferenceManager.getDefaultSharedPreferences(this);
newRequestBool = sharedPref2.getBoolean(SettingsActivity.KEY_PREF_NOTIF_NEW_REQUEST, true);

ReactiveLocationProvider locationProvider = new ReactiveLocationProvider(getBaseContext());
if (ActivityCompat.checkSelfPermission(getBaseContext(), android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getBaseContext(), android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
locationProvider.getLastKnownLocation()
.subscribe(new Action1<Location>() {
@Override
public void call(Location location) {
currentLatDouble = location.getLatitude();
currentLngDouble = location.getLongitude();
geoQuery = geoFire.queryAtLocation(new GeoLocation(currentLatDouble, currentLngDouble), Double.parseDouble(MainActivity.radiusValue));

firebaseDatabaseFollowers.child("child").addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {

if (dataSnapshot.getValue() != null) {

id = dataSnapshot.getKey();

geoQuery.addGeoQueryEventListener(new GeoQueryEventListener() {
@Override
public void onKeyEntered(String key, GeoLocation location) {
firebaseDatabase.child("child").child(key).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot != null) {
Map<String, String> newRequest = (Map<String, String>) dataSnapshot.getValue();
pBy = newRequest.get("pBy");
ss = newRequest.get("ss");

if (String.valueOf(newRequestBool).equals("true")) {
final int m = (int) ((new Date().getTime() / 1000L) % Integer.MAX_VALUE);

Intent notificationIntent = new Intent(getBaseContext(), NotificationARBroadcastReceiver.class);
notificationIntent.putExtra(NotificationARBroadcastReceiver.NOTIFICATION, getNotificationNewRService());
PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), m, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, 0, pendingIntent);

}

} else {
Toast.makeText(getBaseContext(), "null", Toast.LENGTH_SHORT).show();
}
}

...
});

} else {
Toast.makeText(getBaseContext(), "no data", Toast.LENGTH_SHORT).show();
}
}

...
});

}
});

}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return super.onStartCommand(intent, flags, startId);
}

@Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(getBaseContext(), "Service stopped", Toast.LENGTH_SHORT).show();
}

private Notification getNotificationNewRequestService() {

// String id = firebaseDatabaseFollowers.child("game-requests").push().getKey();

mBuilder =
new NotificationCompat.Builder(getBaseContext())
.setSmallIcon(R.mipmap.app_icon_1)
.setContentTitle("Title")
.setContentText("text...");

Intent resultIntent = new Intent(getBaseContext(), Profile.class);

// Because clicking the notification opens a new ("special") activity, there's
// no need to create an artificial back stack.
PendingIntent resultPendingIntent =
PendingIntent.getActivity(
getBaseContext(),
0,
resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT
);

// register receiver in constructor/onCreate()
MyBroadcastReceiver myBroadcastReceiver = new MyBroadcastReceiver();
IntentFilter myIntentFilter = new IntentFilter();
myIntentFilter.addAction(getString(R.string.broadcast_id));
registerReceiver(myBroadcastReceiver, myIntentFilter);

// for action button
Intent actionIntent = new Intent(this, MyBroadcastReceiver.class);
PendingIntent actionPendingIntent = PendingIntent
.getBroadcast(getBaseContext(),
0, actionIntent, PendingIntent.FLAG_UPDATE_CURRENT);

sendBroadcast(actionIntent);

mBuilder.setAutoCancel(true);
// mBuilder.setContentIntent(resultPendingIntent);
mBuilder.addAction(R.drawable.ic_accepted_request_black_24dp, "Accept", actionPendingIntent);
mBuilder.addAction(R.drawable.ic_close_light, "Reject", null);

return mBuilder.build();
}

public class MyBroadcastReceiver extends BroadcastReceiver {

public MyBroadcastReceiver(){
super();
}

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

Log.d("BROADCAST", "Broadcast received");
if (intent.getAction() != null && intent.getAction().equals(getString(R.string.broadcast_id))) {

Intent intent1 = new Intent(MyService.this, MainActivity.class);
startActivity(intent1);

Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
if (String.valueOf(difference).contains("-")) {
// do the logic
} else if (String.valueOf(difference).equals("0")){
// do the logic
} else if (!String.valueOf(differenceCurrentStartTime).contains("-")) {
// do the logic
} else if (!String.valueOf(difference).equals("0") && !String.valueOf(difference).contains("-") && String.valueOf(differenceCurrentStartTime).contains("-")) {
// do the logic

}
}
}, 1000);

}
}
}

}

这是AndroidManifest.xml:

<receiver android:name=".MyBroadcastReceiver">
<intent-filter>
<action android:name="com.abc.ccc.BROADCAST_EVENT">
</action>
</intent-filter>

</receiver>

这是 strings.xml:

// define a Broadcast Intent Action in String resources
<string name="broadcast_id">com.abc.ccc.BROADCAST_EVENT</string>

请告诉我。

最佳答案

您可以使用 BroadcastReceiver 来执行此操作。当用户点击“接受”按钮时,在 BroadcastReceiveronReceive() 中处理其操作。

// for notification itself
Intent notificationIntent = (new Intent()).setAction(INTENT_ACTION_HERE);
PendingIntent notificationPendingIntent = PendingIntent
.getBroadcast(context, NOTIFICATION_INTENT_REQUEST_ID, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

// for action button
Intent actionIntent = new Intent(this, YourBroadcastReceiver.class);
PendingIntent actionPendingIntent = PendingIntent
.getBroadcast(context, ACTION_INTENT_REQUEST_ID, actionIntent, PendingIntent.FLAG_UPDATE_CURRENT);

// execute onNotificationClick
mNotificationBuilder.setContentIntent(notificationPendingIntent);

// execute onButtonClick
mNotificationBuilder.addAction(R.drawable.button_icon, BUTTON_TEXT, actionPendingIntent);

我做了this repository at github更好地理解和实现这一点。

关于android - 在 Notification 的操作按钮上设置 onClickListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41090291/

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