将字符串从一个 class 发送到一个 activity
send String from one class to a activity
我的应用程序使用地理围栏在输入位置时通知用户,当用户输入位置时会显示通知。
我想做的是让触发地理围栏将它们转换为字符串,然后将它们发送到另一个 activity,以便它们可以显示在列表视图中。
我已经在线观看了有关如何创建列表视图以及如何使用 Intent 发送数据的教程,但我尝试做的事情似乎并不奏效。
编辑
我正在使用另一个 class MainActivity 的 startActivity 意图启动列表视图 PageActivity。
目标
如果用户从任何地方(尤其是从应用程序外部)单击通知,它将打开应用程序,并在列表视图中填充地理围栏详细信息。如果用户打开应用程序只是从启动器打开应用程序,那么也会发生同样的情况。要填充任何已触发的地理围栏的列表。
GeofenceTransitionsIntentService Class:
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 Class:
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();
}
});
}
}
非常感谢任何帮助,谢谢。
你还没有开始listPageActivty
activity
替换
Intent i = new Intent(this, listPageActivty.class);
i.putExtra("intentKey",geofenceTransitionDetails);
与
Intent i = new Intent(getBaseContext(), listPageActivty.class);
i.putExtra("intentKey",geofenceTransitionDetails);
getApplication().startActivity(dialogIntent);
你必须改变这个:
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");
我的应用程序使用地理围栏在输入位置时通知用户,当用户输入位置时会显示通知。 我想做的是让触发地理围栏将它们转换为字符串,然后将它们发送到另一个 activity,以便它们可以显示在列表视图中。 我已经在线观看了有关如何创建列表视图以及如何使用 Intent 发送数据的教程,但我尝试做的事情似乎并不奏效。
编辑 我正在使用另一个 class MainActivity 的 startActivity 意图启动列表视图 PageActivity。
目标
如果用户从任何地方(尤其是从应用程序外部)单击通知,它将打开应用程序,并在列表视图中填充地理围栏详细信息。如果用户打开应用程序只是从启动器打开应用程序,那么也会发生同样的情况。要填充任何已触发的地理围栏的列表。
GeofenceTransitionsIntentService Class:
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 Class:
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();
}
});
}
}
非常感谢任何帮助,谢谢。
你还没有开始listPageActivty
activity
替换
Intent i = new Intent(this, listPageActivty.class);
i.putExtra("intentKey",geofenceTransitionDetails);
与
Intent i = new Intent(getBaseContext(), listPageActivty.class);
i.putExtra("intentKey",geofenceTransitionDetails);
getApplication().startActivity(dialogIntent);
你必须改变这个:
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");