<?xml version="1.0" encoding="utf-8"?>
<manifest package="it.alcacoop.backgammon">
<uses-sdk android:minSdkVersion="5"
android:targetSdkVersion="17" />
<application>
<activity>
</activity>
</application>
</manifest>
android create project \
--target <target_ID> \
--name <your_project_name> \
--path path/to/your/project \
--activity <your_activity_name> \
--package <your_package_namespace>
AndroidManifest.xml
bin
gen
proguard.cfg
project.properties
res
src
drawable-hdpi
drawable-ldpi
drawable-mdpi
layout
raw
values
<uses-permission
android:name="android.permission.INTERNET"/>
<uses-permission
android:name="android.permission.ACCESS_NETWORK_STATE"/>
private boolean isNetworkAvailable() {
ConnectivityManager connectivityManager =
getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo =
connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null;
}
Android fornisce un robusto modello di sicurezza, caratterizzato da diversi livelli di implementazione e granularità a livello applicativo
<application>
<activity android:name=".ExampleActivity"/>
<activity android:name=".ExampleActivity2"/>
...
</application
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
Activity Lifecycle Methods | |||
---|---|---|---|
Method | Description | Killable | Next |
onCreate() | Called when activity first created | No | onStart() |
onRestart() | Called after activity stopped, prior to restarting | No | onStart() |
onStart() | Called when activity is becoming visible to user | No | onResume()/onStop() |
onResume() | Called when activity starts interacting with user | No | onPause() |
onPause() | Called when a previous activity is about to resume | Yes | onResume()/onStop() |
onStop() | Called when activity no longer visible to user | Yes | onRestart()/onDestroy() |
onDestroy() | Final call received before activity is destroyed | Yes | Nothing |
Reimplementiamo tutti gli hook che gestiscono il ciclo di vita di una activity con delle semplici print (Log.*)...
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.e("LIFECYCLE", "CREATE!!!");
}
@Override
public void onRestart() {
super.onRestart();
Log.e("LIFECYCLE", "RESTART!!!");
}
...
adb shell #apre una shell sul dispositivo
ps #mostra la lista dei processi attivi
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
<Button
android:id="@+id/create_dialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Open Dialog!" />
Nota: è sempre necessario assegnare un ID ai compenenti che si aggiungono, attraverso il quale sarà possibile referenziare l'oggetto nel codice Java
final Button button =
(Button) findViewById(R.id.create_dialog);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//DO SOMETHING
}
});
private AlertDialog dialog;
public void createDialog() {
AlertDialog.Builder builder =
new AlertDialog.Builder(this);
builder.setMessage("HELLO!")
.setTitle("TEST DIALOG")
.setPositiveButton("OK", null);
dialog = builder.create();
}
onCreate() {
setContentView...
createDialog...
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.show();
}
});
}
Layout:
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello_world"
string.xml
<resources>
<string name="hello_world">
Hello World from MainActivity
</string>
</resources>
Accedere programmaticamente ad una stringa:
(String)getResources().getText(R.string.hello_world)
/** Creazione di una Intent esplicita
*
* param Context Application Context
* param Class Component Class
*/
Intent myIntent = new Intent(this, MainActivity.class);
Intent myIntent = new Intent(
Intent.ACTION_VIEW,
Uri.parse("http://google.it")
);
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.BROWSABLE"/>
<data android:scheme="http"/>
<data android:scheme="https"/>
<data android:host="youtube.com"/>
<data android:host="www.youtube.com"/>
<data android:host="m.youtube.com"/>
<data android:pathPrefix="/watch"/>
<data android:pathPrefix="/v/"/>
<data android:pathPrefix="/e/"/>
<data android:pathPrefix="/embed/"/>
</intent-filter>
<Button
android:id="@+id/open_google"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send Intent!"
android:onClick="sendIntent"/>
public void sendIntent(View view) {
Intent myIntent = new Intent(
Intent.ACTION_VIEW,
Uri.parse("http://google.it")
);
startActivity(myIntent);
}
class MyThread extends Thread {
public void run() {
//LOGICA DI ESECUZIONE
}
}
MyThread myThread = new MyThread();
myThread.start(); //LANCIA IL THREAD
Thread(Runnable r)
class MyRunnable implements Runnable {
public void run() {
//LOGICA DI ESECUZIONE
}
}
MyRunnable r = new MyRunnable();
Thread myThread = new Thread(r);
myThread.start(); //LANCIA IL THREAD
//LONG TASK
public void longTask() {
File root = Environment.getExternalStorageDirectory();
File file = new File(root, "test.txt");
try {
if (root.canWrite()){
FileWriter filewriter = new FileWriter(file);
BufferedWriter out = new BufferedWriter(filewriter);
for (int i=0; i<1000000; i++) out.write(i);
out.close();
}
} catch (IOException e) {}
}
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
longTask();
dialog.show();
}
});
public void onClick(View v) {
Thread t = new Thread() {
@Override
public void run() {
longTask();
dialog.show();
}
};
t.start();
}
public void onClick(View v) {
Thread t = new Thread() {
@Override
public void run() {
longTask();
runOnUiThread(new Runnable() {
@Override
public void run() {
dialog.show();
}
});
}
};
t.start();
}