gpt4 book ai didi

java - 将字符串从一个类发送到一个 Activity

转载 作者:行者123 更新时间:2023-12-01 10:29:07 25 4
gpt4 key购买 nike

我的应用程序使用地理围栏在输入位置时通知用户,当用户输入该位置时会显示通知。我试图做的是让触发地理围栏将它们转换为字符串,然后将它们发送到另一个 Activity ,以便它们可以显示在 ListView 中。我已经在线观看了有关如何制作 ListView 以及如何使用 Intent 发送数据的教程,但我尝试做的事情似乎不起作用。

编辑我正在使用另一个类 MainActivity 中的 startActivity Intent 来启动 ListView PageActivity。

目标

如果用户从任何地方(尤其是从应用程序外部)单击通知,它将打开应用程序,并在 ListView 中填充地理围栏详细信息。如果用户打开应用程序只是从启动器打开应用程序,那么也会发生同样的情况。该列表将填充任何已触发的地理围栏。

GeofenceTransitionsIntentService 类:

public class GeofenceTransitionsIntentService extends IntentService {

protected static final String TAG = "GeofenceTransitionsIS";



/**
* This constructor is required, and calls the super IntentService(String)
* constructor with the name for a worker thread.
*/
public GeofenceTransitionsIntentService() {
// Use the TAG to name the worker thread.
super(TAG);
}


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

/**
* Handles incoming intents.
* @param intent sent by Location Services. This Intent is provided to Location
* Services (inside a PendingIntent) when addGeofences() is called.
*/
@Override
protected void onHandleIntent(Intent intent) {

GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent);
if (geofencingEvent.hasError()){
String errorMessage = GeofenceErrorMessages.getErrorString(this,
geofencingEvent.getErrorCode());
Log.e(TAG, errorMessage);
return;
}

// Get the Transistion Type
int geofenceTransition = geofencingEvent.getGeofenceTransition();

// Test that the reported transisition was of interest.
if (geofenceTransition == Geofence.GEOFENCE_TRANSITION_ENTER ||
geofenceTransition == Geofence.GEOFENCE_TRANSITION_EXIT) {

// Get the geofences that were triggered. A single event can trigger multiple geofences.
List<Geofence> triggeringGeofences = geofencingEvent.getTriggeringGeofences();

// Get the transistion details as a String.
String geofenceTransitionDetails = getGeofenceTransitionDetails(
this,
geofenceTransition,
triggeringGeofences
);


headsUpSmsNotifaction(geofenceTransitionDetails);


Intent i = new Intent(this, listPageActivty.class);
i.putExtra("intentKey",geofenceTransitionDetails);



Log.i(TAG, geofenceTransitionDetails);
} else{
// Log the error
Log.e(TAG, getString(R.string.geofence_transition_invalid_type, geofenceTransition));

}

}


/**
* Gets transition details and returns them as a formatted string.
*
* @param context The app context.
* @param geofenceTransition The ID of the geofence transition.
* @param triggeringGeofences The geofence(s) triggered.
* @return The transition details formatted as String.
*/

private String getGeofenceTransitionDetails(Context context, int geofenceTransition, List<Geofence> triggeringGeofences) {
String geofenceTransitionString = getTransitionString(geofenceTransition);

// Get the Ids of each geofence that was triggered.
ArrayList triggeringGeofencesIdsList = new ArrayList();
for (Geofence geofence : triggeringGeofences) {
triggeringGeofencesIdsList.add(geofence.getRequestId());
}

String triggeringGeofencesIdsString = TextUtils.join(", ", triggeringGeofencesIdsList);


return geofenceTransitionString + ": " + triggeringGeofencesIdsString;
}

/**
* Posts a notification in the notification bar when a transition is detected.
* If the user clicks the notification, control goes to the MainActivity.
*/


private void headsUpSmsNotifaction(String notificationDetails){

// Create an explicit content Intent that starts the main Activity.
Intent notificationIntent = new Intent(this, sendSmsReceiver.class);
PendingIntent notificationPendingIntent = PendingIntent.getBroadcast(this,44,notificationIntent,0);

Calendar calendar = Calendar.getInstance();
SimpleDateFormat mdfromat = new SimpleDateFormat("hh:ss | dd/ MM / yyyy");
String strDate = mdfromat.format(calendar.getTime());



// Get a notification builder that's compatible with platform versions >= 4
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);

// Define the notification settings.
builder.setSmallIcon(R.mipmap.ic_launcher)
// In a real app, you may want to use a library like Volley
// to decode the Bitmap.
.setLargeIcon(BitmapFactory.decodeResource(getResources(),
R.mipmap.ic_launcher))
.setColor(Color.RED)
.setPriority(NotificationCompat.PRIORITY_MAX)
.setVibrate(new long[]{10,10,10})
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setContentTitle(notificationDetails)
.setContentText(strDate)
.addAction(R.mipmap.ic_launcher,"send",notificationPendingIntent)

.setContentIntent(notificationPendingIntent);


// Dismiss notification once the user touches it.
//builder.setAutoCancel(true);

// Get an instance of the Notification manager
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

// Issue the notification
mNotificationManager.notify(0, builder.build());
}



/**
* Maps geofence transition types to their human-readable equivalents.
*
* @param transitionType A transition type constant defined in Geofence
* @return A String indicating the type of transition
*/

private String getTransitionString(int transitionType) {
switch (transitionType) {
case Geofence.GEOFENCE_TRANSITION_ENTER:
return getString(R.string.geofence_transition_entered);
case Geofence.GEOFENCE_TRANSITION_EXIT:
return getString(R.string.geofence_transition_exited);
default:
return getString(R.string.unknown_geofence_transition);
}

}
}

listPageActivty 类:

public class listPageActivty extends AppCompatActivity {

ListView listView;
String[] ListElements = new String[]{"Android","PHP"};
String item = "one";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_page_activty);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);


listView = (ListView) findViewById(R.id.listView2);
Button addBtn = (Button) findViewById(R.id.addBtn);

final List<String> ListElementsArrayList = new ArrayList<String>(Arrays.asList(ListElements));
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(listPageActivty.this,android.R.layout.simple_list_item_1,ListElementsArrayList);

listView.setAdapter(adapter);

Bundle bundleData = getIntent().getExtras();
if (bundleData == null){
return;
}

String intentItem = bundleData.getString("intentKey");
ListElementsArrayList.add(intentItem);


addBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

ListElementsArrayList.add(item);
adapter.notifyDataSetChanged();
}
});


}

}

非常感谢任何帮助,谢谢。

最佳答案

你必须改变这个:

Intent i = new Intent(this, listPageActivty.class);
i.putExtra("intentKey",geofenceTransitionDetails);

对此:

Intent i = new Intent(this, listPageActivty.class);
i.putExtra("intentKey",geofenceTransitionDetails);
this.startActivity(i);

我认为您刚刚离开了startActivity()

在另一个Activity中,您将获得如下值:

String GeoTransitionDetails = getIntent().getExtras().getString("intentKey");

关于java - 将字符串从一个类发送到一个 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35187518/

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