-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathMainActivity.java
More file actions
executable file
·100 lines (86 loc) · 4.53 KB
/
MainActivity.java
File metadata and controls
executable file
·100 lines (86 loc) · 4.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// **********************************************************************************************
// * *
// * This application is intended for demonstration purposes only. It is provided as-is *
// * without guarantee or warranty and may be modified to suit individual needs. *
// * *
// **********************************************************************************************
package com.zebra.basicintent1;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
//
// The section snippet below registers to receive the data broadcast from the
// DataWedge intent output. In the example, a dynamic broadcast receiver is
// registered in the onCreate() call of the target app. Notice that the filtered action
// matches the "Intent action" specified in the DataWedge Intent Output configuration.
//
// For a production app, a more efficient way to the register and unregister the receiver
// might be to use the onResume() and onPause() calls.
// Note: If DataWedge had been configured to start an activity (instead of a broadcast),
// the intent could be handled in the app's manifest by calling getIntent() in onCreate().
// If configured as startService, then a service must be created to receive the intent.
//
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
IntentFilter filter = new IntentFilter();
filter.addCategory(Intent.CATEGORY_DEFAULT);
filter.addAction(getResources().getString(R.string.activity_intent_filter_action));
registerReceiver(myBroadcastReceiver, filter);
}
@Override
protected void onDestroy()
{
super.onDestroy();
unregisterReceiver(myBroadcastReceiver);
}
//
// After registering the broadcast receiver, the next step (below) is to define it.
// Here it's done in the MainActivity.java, but also can be handled by a separate class.
// The logic of extracting the scanned data and displaying it on the screen
// is executed in its own method (later in the code). Note the use of the
// extra keys defined in the strings.xml file.
//
private BroadcastReceiver myBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Bundle b = intent.getExtras();
// This is useful for debugging to verify the format of received intents from DataWedge
//for (String key : b.keySet())
//{
// Log.v(LOG_TAG, key);
//}
if (action.equals(getResources().getString(R.string.activity_intent_filter_action))) {
// Received a barcode scan
try {
displayScanResult(intent, "via Broadcast");
} catch (Exception e) {
// Catch if the UI does not exist when we receive the broadcast
}
}
}
};
//
// The section below assumes that a UI exists in which to place the data. A production
// application would be driving much of the behavior following a scan.
//
private void displayScanResult(Intent initiatingIntent, String howDataReceived)
{
String decodedSource = initiatingIntent.getStringExtra(getResources().getString(R.string.datawedge_intent_key_source));
String decodedData = initiatingIntent.getStringExtra(getResources().getString(R.string.datawedge_intent_key_data));
String decodedLabelType = initiatingIntent.getStringExtra(getResources().getString(R.string.datawedge_intent_key_label_type));
final TextView lblScanSource = (TextView) findViewById(R.id.lblScanSource);
final TextView lblScanData = (TextView) findViewById(R.id.lblScanData);
final TextView lblScanLabelType = (TextView) findViewById(R.id.lblScanDecoder);
lblScanSource.setText(decodedSource + " " + howDataReceived);
lblScanData.setText(decodedData);
lblScanLabelType.setText(decodedLabelType);
}
}