public class SGActivity extends Activity implements SGAgent.SGClient
SGActivity is a sample implementation that demonstrates usage of SGAgent. You may inherit this class to save your time. SGActivity implements SGClient interface, but most methods are dummy, you should override them to make them work. If you cannot inherit SGActivity in case that you inherit other Activity implementations provided by Unity and Flash engines, you may create your own implementation on the base of SGActivity. Here comes the code of SGActivity.
public class SGActivity extends Activity implements SGAgent.SGClient {
protected SGAgent agent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
onPreInitialization(savedInstanceState);
agent = new SGAgent();
agent.onCreate(this, this);
onPostInitialization(savedInstanceState);
}
@Override
protected void onStart() {
super.onStart();
agent.onStart();
}
@Override
protected void onRestart() {
super.onRestart();
agent.onRestart();
}
@Override
protected void onResume() {
super.onResume();
agent.onResume();
}
@Override
protected void onPause() {
agent.onPause();
super.onPause();
}
@Override
protected void onStop() {
agent.onStop();
super.onStop();
}
@Override
protected void onDestroy() {
agent.onDestroy();
super.onDestroy();
System.exit(0);
}
@Override
public void onNewIntent(Intent newIntent) {
agent.onNewIntent(newIntent);
super.onNewIntent(newIntent);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (agent.onKeyDown(keyCode, event))
return true;
return super.onKeyDown(keyCode, event);
}
@Override
public void onBackPressed() {
agent.onBackPressed();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
agent.onActivityResult(requestCode, resultCode, data);
super.onActivityResult(requestCode, resultCode, data);
}
@TargetApi(23)
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
agent.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
public void onLog(String text) {
}
public void onLog(String text, String postfix) {
}
public void onPlayerDetails(String details) {
}
public boolean onProductBegin(int num) {
return true;
}
public void onProductFound(UProduct product) {
}
public void onProductEnd() {
}
public boolean onOrderInfoBegin() {
return true;
}
public void onOrderInfoFound(UOrderInfo orderInfo)
{
}
public void onOrderInfoEnd() {
}
public void onTreasureListFound(String[] treasures) {
}
public void onTreasureChange(String name, int count) {
}
public void onLogValue(String key, int value) {
}
public void onOrderStateChange(String productID, String orderID, String state, String message) {
}
public void onStateChange(int type, int orgState, int newState, int reason, String arg) {
}
public void onExtMethodNotification(String cookie, int code, String msg) {
}
public void onExitCancelled() {
}
public void onFuncRequest(String func, String id, String arg) {
}
protected void onPreInitialization(Bundle savedInstanceState) {
}
protected void onPostInitialization(Bundle savedInstanceState) {
}
Please always call System.exit to exit process after application (MainActivity) is closed. Otherwise applications may behave bad due to static data are not initialized before MainActivity is opened again (Activity is closed but application process still exists).
Modifier and Type | Field and Description |
---|---|
protected SGAgent |
agent
SGAgent instance.
|
Constructor and Description |
---|
SGActivity() |
Modifier and Type | Method and Description |
---|---|
protected void |
onActivityResult(int requestCode,
int resultCode,
Intent data)
Override onActivityResult of Activity.
|
void |
onBackPressed()
Override onBackPressed of Activity.
|
protected void |
onCreate(Bundle savedInstanceState)
Overrides onCreate of Activity.
|
protected void |
onDestroy()
Override onDestroy of Activity.
|
void |
onExitCancelled()
Notify that the exit operation was cancelled.
|
void |
onExtMethodNotification(String cookie,
int code,
String msg)
Notify the result of asynchronous function.
|
void |
onFuncRequest(String func,
String id,
String arg)
Notify client the underlying layer request application to execute a function.
|
boolean |
onKeyDown(int keyCode,
KeyEvent event)
Override onKeyDown of Activity.
|
void |
onLog(String text)
Display debug message.
|
void |
onLog(String text,
String postfix)
Display debug message.
|
void |
onLogValue(String key,
int value,
String msg)
Inform client value of remote log key.
|
void |
onNewIntent(Intent newIntent)
Override onNewIntent of Activity.
|
boolean |
onOrderInfoBegin(int num)
Inform client that order information notification will be started.
|
void |
onOrderInfoEnd()
Inform client that order information notification is finished.
|
void |
onOrderInfoFound(UOrder order)
Send order information to client.
|
void |
onOrderStateChange(String productID,
int num,
String orderID,
String state,
String message)
Notify client the state change of product purchase.
|
protected void |
onPause()
Override onPause of Activity.
|
void |
onPlayerDetails(String details)
Notify player details.
|
void |
onPostInitialization(Bundle savedInstanceState) |
void |
onPreInitialization(Bundle savedInstanceState) |
boolean |
onProductBegin(int num)
Inform client that product information notification will be started.
|
void |
onProductEnd()
Inform client that product information notification is finished.
|
void |
onProductFound(UProduct product)
Send product information to client.
|
void |
onRequestPermissionsResult(int requestCode,
String[] permissions,
int[] grantResults)
Override onRequestPermissionsResult of Activity.
|
protected void |
onRestart()
Override onRestart of Activity.
|
protected void |
onResume()
Override onResume of Activity.
|
protected void |
onStart()
Override onStart of Activity.
|
void |
onStateChange(int type,
int orgState,
int newState,
int reason,
int op,
String arg)
Notify client the internal state change in SGAgent.
|
protected void |
onStop()
Override onStop of Activity.
|
void |
onTreasureChange(String name,
int count)
Inform client updated treasure count.
|
void |
onTreasureListFound(String[] treasures)
Inform client that treasure list is retrieved.
|
protected SGAgent agent
protected void onActivityResult(int requestCode, int resultCode, Intent data)
requestCode
- Standard Android parameterresultCode
- Standard Android parameterdata
- Standard Android parameterpublic void onBackPressed()
protected void onCreate(Bundle savedInstanceState)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); // Call onCreate of super class
onPreInitialization(savedInstanceState); // Give derived class a chance to do something before initialization. Because agent.onCreate may output logs to UI, UI components should be created in advance.
agent = new SGAgent(); // create SGAgent instance
agent.onCreate(this, this); // call agent.onCreate to initialize
onPostInitialization(savedInstanceState);// Give derived class a chance to do something after initialization.
}
savedInstanceState
- Standard Android parameterprotected void onDestroy()
public void onExitCancelled()
SGAgent.SGClient
onExitCancelled
in interface SGAgent.SGClient
public void onExtMethodNotification(String cookie, int code, String msg)
SGAgent.SGClient
onExtMethodNotification
in interface SGAgent.SGClient
cookie
- string for identification passed when run the functioncode
- result code. One of EXT_RET_OK, EXT_RET_ERROR and EXT_RET_CANCELLED.msg
- content of notification. The format depends on function implementation.public void onFuncRequest(String func, String id, String arg)
SGAgent.SGClient
onFuncRequest
in interface SGAgent.SGClient
func
- Function name. Should be on of "screenshot" and "logout".id
- Operation ID. Application may use it to perform multiple operations.arg
- Argument. For "screenshot", it is the file path to which the screen shot should be saved to.
Notify that the exit operation was cancelled. Application should recover current screen and go on.public boolean onKeyDown(int keyCode, KeyEvent event)
keyCode
- Standard Android parameterevent
- Standard Android parameterpublic void onLog(String text)
SGAgent.SGClient
onLog
in interface SGAgent.SGClient
text
- message to displaypublic void onLog(String text, String postfix)
SGAgent.SGClient
onLog
in interface SGAgent.SGClient
text
- message to displaypostfix
- postfix to be appended after textpublic void onLogValue(String key, int value, String msg)
SGAgent.SGClient
onLogValue
in interface SGAgent.SGClient
key
- key to matchvalue
- value of key or specified default valuemsg
- content text of the logpublic void onNewIntent(Intent newIntent)
newIntent
- Standard Android parameterpublic boolean onOrderInfoBegin(int num)
SGAgent.SGClient
onOrderInfoBegin
in interface SGAgent.SGClient
num
- number of orderspublic void onOrderInfoEnd()
SGAgent.SGClient
onOrderInfoEnd
in interface SGAgent.SGClient
public void onOrderInfoFound(UOrder order)
SGAgent.SGClient
onOrderInfoFound
in interface SGAgent.SGClient
order
- order information, null indicating no more information is availablepublic void onOrderStateChange(String productID, int num, String orderID, String state, String message)
SGAgent.SGClient
onOrderStateChange
in interface SGAgent.SGClient
productID
- product IDnum
- number of copiesorderID
- order IDstate
- state. One of the following text strings:protected void onPause()
public void onPlayerDetails(String details)
SGAgent.SGClient
onPlayerDetails
in interface SGAgent.SGClient
details
- player detailspublic void onPostInitialization(Bundle savedInstanceState)
public void onPreInitialization(Bundle savedInstanceState)
public boolean onProductBegin(int num)
SGAgent.SGClient
onProductBegin
in interface SGAgent.SGClient
num
- number of productspublic void onProductEnd()
SGAgent.SGClient
onProductEnd
in interface SGAgent.SGClient
public void onProductFound(UProduct product)
SGAgent.SGClient
onProductFound
in interface SGAgent.SGClient
product
- product information, null indicating no more information is availablepublic void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults)
requestCode
- Standard Android parameterpermissions
- Standard Android parametergrantResults
- Standard Android parameterprotected void onRestart()
protected void onResume()
protected void onStart()
public void onStateChange(int type, int orgState, int newState, int reason, int op, String arg)
SGAgent.SGClient
onStateChange
in interface SGAgent.SGClient
type
- state type. One of STATE_TYPE_USR, STATE_TYPE_JOB and STATE_TYPE_PAY.orgState
- original statenewState
- new statereason
- reason of state change. One of STATE_CHANGE_REASON_NONE,
STATE_CHANGE_REASON_OP,
STATE_CHANGE_REASON_OP_SUCCESS,
STATE_CHANGE_REASON_OP_FAILURE and
STATE_CHANGE_REASON_OP_CANCELED.op
- operation leading to the changearg
- extra string information. arg is set to the value of parameter imageFile passed to SGAgent.shareScreen by application.
Application may use this string to delete temporary image file created for sharing.protected void onStop()
public void onTreasureChange(String name, int count)
SGAgent.SGClient
onTreasureChange
in interface SGAgent.SGClient
name
- treasure namecount
- treasure countpublic void onTreasureListFound(String[] treasures)
SGAgent.SGClient
onTreasureListFound
in interface SGAgent.SGClient
treasures
- Treasure array. Each item of the array has format "name=count",
where 'count' is an integer indicating number of a certain treasure.