An activity has essentially four states
In order to react on lifecycle state changes, override these methods
public class Activity extends Context {
protected void onCreate(Bundle savedInstanceState);
protected void onStart();
protected void onRestart();
protected void onResume();
protected void onPause();
protected void onStop();
protected void onDestroy();
}
...
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
...
}
// lauches a new activity
abstract void startActivity(Intent intent)
//launches a new service
abstract ComponentName startService(Intent service)
//Registers a BroadCast Receiver
abstract Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter)
Intent dialIntent = new Intent();
dialIntent.setAction(Intent.ACTION_DIAL); //specifiy the action to execute
dialIntent.setData(Uri.parse("tel:0711-1234")); //provide action data
startActivity(dialIntent);
1. Activity A creates an Intent with an action description and passes it to startActivity()
2. The Android System searches all apps for an intent filter that matches the intent
3. When a match is found, the system starts the matching activity (Activity B) by invoking its onCreate() method and passing it the Intent.
<intent-filter>
element in your manifest file.<intent-filter>
element in the app’s manifest file, nested in the corresponding app component (such as an <activity>
element).<intent-filter>
, you can specify the type of intents to accept using one or more of these three elements:<action>
Declares the intent action accepted, in the name attribute. The value must be the literal string value of an action, not the class constant.
<data>
Declares the type of data accepted, using one or more attributes that specify various aspects of the data URI (scheme, host, port, path) and MIME type.
<category>
Declares the intent category accepted, in the name attribute. The value must be the literal string value of an action, not the class constant.
<activity android:name="MainActivity">
<!-- This activity is the main entry, should appear in app launcher -->
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Note:To receive implicit intents, you must include the CATEGORY_DEFAULT category in the intent filter
Intent intent = new Intent (getApplicationContext(), LoginActivity.class);
intent.putExtra("Name","HdM-Login");
startActivity(intent);
...
Note: An explicit intent is always delivered to its target, regardless of any intent filters the component declares.
Application sandboxing (process separation)
Permissions
<uses-permission>
tags app manifest must be used
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.app.myapp" >
<uses-permission android:name="android.permission.RECEIVE_SMS" />
...
</manifest>
To request a dangerous permission at runtime, some additional code is required in your Activity class:
// Assume thisActivity is the current activity
int permissionCheck = ContextCompat.checkSelfPermission(thisActivity,
Manifest.permission.WRITE_CALENDAR);
...
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
Manifest.permission.READ_CONTACTS)) {
// Show an explanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
} else {
... //see next slide
...
else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(thisActivity,
new String[]{Manifest.permission.READ_CONTACTS},
MY_PERMISSIONS_REQUEST_READ_CONTACTS);
// MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
// app-defined int constant. The callback method gets the
// result of the request.
}
...
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_READ_CONTACTS: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted, yay! Do the
// contacts-related task you need to do.
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
}
return;
}}}
Uri uri = Uri.parse("http://www.hdm-stuttgart.de");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
<WebView
android:id="@+id/webView1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
WebView myWebView = (WebView) findViewById(R.id.webview1);
myWebView.loadUrl("http://www.hdm-stuttgart.de");
<uses-permission android:name="android.permission.INTERNET" />
myWebView.setWebViewClient(new WebViewClient(){});
webview.getSettings().setJavaScriptEnabled(true);
WebView myWebView = (WebView) findViewById(R.id.webView1);
myWebView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
ProgressBar bar = (ProgressBar) findViewById(R.id.progressBar);
bar.setProgress(progress);
}
});
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content" />