This is a simple Activity which has an input field and a button. When the button is pressed the value is stored as a "preference" and the Activity is closed. The input field and its labels and the button are defined in one or more xml files in res/layout.
package com.organization.package;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class purposeActivity extends Activity {
private static final String TAG = purposeActivity.class.getSimpleName();
private static final String PREFS = "packageConfig";
static SharedPreferences preferences = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.purpose);
Log.i(TAG, "onCreate ");
preferences = getSharedPreferences(PREFS,MODE_PRIVATE);
final Button button = (Button) findViewById(R.id.submitButton);
button.setText("Submit");
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
SharedPreferences.Editor editor = preferences.edit();
Log.i(TAG, "Purpose button clicked.");
// Perform action on click
String myPurpose = new String(((EditText) findViewById(R.id.purpose)).getText().toString().trim());
editor.putString("Purpose", myPurpose );
editor.commit();
finish();
}
});
}
@Override
public void onResume() {
super.onResume();
Log.i(TAG, "at onResume");
TextView t;
t = (TextView) findViewById(R.id.topLine);
t.setText("Package by Organization" );
}
@Override
public void onDestroy() {
super.onDestroy();
}
}
This is a sevice that does nothing but write log entries. A good use for a started might be something time consuming such as getting a GPS fix.
package com.organization.package;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
public class purposeService extends Service {
private static final String TAG = purposeService.class.getSimpleName() ;
/* This is mandatory, and not needed for a started service. */
@Override
public IBinder onBind(Intent intent) { return null; }
@Override
public void onCreate() {
Log.v(TAG, "onCreate");
super.onCreate();
/* This will be called repeatedly. */
/* For one time initialization --
declare initialized items static,
and verify that they have not been
initialized before initializing them. */
}
@Override
public void onDestroy() {
Log.v(TAG, "onDestroy");
super.onDestroy();
/* put your toys away */
}
@Override
public void onStart(Intent intent, int startId) {
Log.i(TAG, "onStart");
super.onStart(intent, startId);
/* This is where the most useful code goes */
}
}