gpt4 book ai didi

java - 单击通知后更改类 Android

转载 作者:太空狗 更新时间:2023-10-29 15:07:13 26 4
gpt4 key购买 nike

我正在尝试将我的类(class)更改为转到 Login.java 我已经尝试了很多解决方案,但似乎无法得到它。我有一个 PromximityAlert 设置来检查位置,在确认该位置后,它将显示通知,当单击该通知时,它将启动登录类。

这是我的代码:

public class ProxAlertActivity extends Activity {

private static final long MINIMUM_DISTANCECHANGE_FOR_UPDATE = 1; // in Meters
private static final long MINIMUM_TIME_BETWEEN_UPDATE = 1000; // in Milliseconds

private static final long POINT_RADIUS = 1000; // in Meters
private static final long PROX_ALERT_EXPIRATION = -1;

private static final String POINT_LATITUDE_KEY = "POINT_LATITUDE_KEY";
private static final String POINT_LONGITUDE_KEY = "POINT_LONGITUDE_KEY";

private static final String PROX_ALERT_INTENT = "com.example.mysqltest.ProximityAlert";

private static final NumberFormat nf = new DecimalFormat("##.########");

private LocationManager locationManager;

private EditText latitudeEditText;
private EditText longitudeEditText;
private Button findCoordinatesButton;
private Button savePointButton;

@Override
public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.main);

locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MINIMUM_TIME_BETWEEN_UPDATE,
MINIMUM_DISTANCECHANGE_FOR_UPDATE,
new MyLocationListener()
);

latitudeEditText = (EditText) findViewById(R.id.point_latitude);
longitudeEditText = (EditText) findViewById(R.id.point_longitude);
findCoordinatesButton = (Button) findViewById(R.id.find_coordinates_button);
savePointButton = (Button) findViewById(R.id.save_point_button);

findCoordinatesButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
populateCoordinatesFromLastKnownLocation();
}
});

savePointButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
saveProximityAlertPoint();
}
});

}

private void saveProximityAlertPoint() {
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location==null) {
Toast.makeText(this, "No last known location. Aborting...", Toast.LENGTH_LONG).show();
return;
}
saveCoordinatesInPreferences((float)location.getLatitude(), (float)location.getLongitude());
addProximityAlert(location.getLatitude(), location.getLongitude());
}

private void addProximityAlert(double latitude, double longitude) {

Intent intent = new Intent(PROX_ALERT_INTENT);
PendingIntent proximityIntent = PendingIntent.getBroadcast(this, 0, intent, 0);

locationManager.addProximityAlert(
41.69519672, // the latitude of the central point of the alert region
-87.80026184, // the longitude of the central point of the alert region
POINT_RADIUS, // the radius of the central point of the alert region, in meters
PROX_ALERT_EXPIRATION, // time for this proximity alert, in milliseconds, or -1 to indicate no expiration
proximityIntent // will be used to generate an Intent to fire when entry to or exit from the alert region is detected
);

IntentFilter filter = new IntentFilter(PROX_ALERT_INTENT);
registerReceiver(new ProximityIntentReceiver(), filter);

}

private void populateCoordinatesFromLastKnownLocation() {
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location!=null) {
latitudeEditText.setText(nf.format(location.getLatitude()));
longitudeEditText.setText(nf.format(location.getLongitude()));
}
}

private void saveCoordinatesInPreferences(float latitude, float longitude) {
SharedPreferences prefs = this.getSharedPreferences(getClass().getSimpleName(), Context.MODE_PRIVATE);
SharedPreferences.Editor prefsEditor = prefs.edit();
prefsEditor.putFloat(POINT_LATITUDE_KEY, latitude);
prefsEditor.putFloat(POINT_LONGITUDE_KEY, longitude);
prefsEditor.commit();
}

private Location retrievelocationFromPreferences() {
SharedPreferences prefs = this.getSharedPreferences(getClass().getSimpleName(), Context.MODE_PRIVATE);
Location location = new Location("POINT_LOCATION");
location.setLatitude(prefs.getFloat(POINT_LATITUDE_KEY, 0));
location.setLongitude(prefs.getFloat(POINT_LONGITUDE_KEY, 0));
return location;
}

public class MyLocationListener implements LocationListener {
public void onLocationChanged(Location location) {
Location pointLocation = retrievelocationFromPreferences();
float distance = location.distanceTo(pointLocation);
Toast.makeText(ProxAlertActivity.this,
"Distance from Point:"+distance, Toast.LENGTH_LONG).show();
}
public void onStatusChanged(String s, int i, Bundle b) {
}
public void onProviderDisabled(String s) {
}
public void onProviderEnabled(String s) {
}
}
}

public class ProximityIntentReceiver extends BroadcastReceiver {


private static final int NOTIFICATION_ID = 1000;

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

String key = LocationManager.KEY_PROXIMITY_ENTERING;

Boolean entering = intent.getBooleanExtra(key, false);

if (entering) {
Log.d(getClass().getSimpleName(), "entering");
}
else {
Log.d(getClass().getSimpleName(), "exiting");
}


NotificationManager notificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);


PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);

Notification notification = createNotification();
notification.setLatestEventInfo(context, "Proximity Alert!", "You are near your point of interest.", pendingIntent);

notificationManager.notify(NOTIFICATION_ID, notification);


}
private Notification createNotification() {
Notification notification = new Notification();

notification.icon = R.drawable.ic_menu_notifications;
notification.when = System.currentTimeMillis();


notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.flags |= Notification.FLAG_SHOW_LIGHTS;

notification.defaults |= Notification.DEFAULT_VIBRATE;
notification.defaults |= Notification.DEFAULT_LIGHTS;

notification.ledARGB = Color.WHITE;
notification.ledOnMS = 1500;
notification.ledOffMS = 1500;

return notification;
}
}

最佳答案

添加这个,我认为它丢失了

Intent intent = new Intent(this, NotificationReceiver.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);

notification.setContentIntent(pendingIntent );

关于java - 单击通知后更改类 Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20765746/

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