The document discusses Android development and UI design. It introduces some common widgets in Android like TextView, buttons, and different layouts like linear, relative and table layouts. It also discusses activities, services, intents and the Android component and manifest files.
19. 类别
package com.moko.hello;
import com.moko.hello.R;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class HelloMoko extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
Android Copyright (c) 2009 Jollen’s Consulting . www.jollen.org/consulting
20. public class HelloMoko extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
onCreate() View
Android Copyright (c) 2009 Jollen’s Consulting . www.jollen.org/consulting
27. public class HelloMoko extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
tv.setText("Hello Moko");
setContentView(tv);
}
}
写
Android Copyright (c) 2009 Jollen’s Consulting . www.jollen.org/consulting
30. 执
统 kernel
个apk 个
process 执
Android 个 Linux
process
Android 统 process
Android 决
统 决
Android Copyright (c) 2009 Jollen’s Consulting . www.jollen.org/consulting
31. 类别
package com.moko.hello;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
public class MokoService extends Service {
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
}
}
Android Copyright (c) 2009 Jollen’s Consulting . www.jollen.org/consulting
32. 类别
没 UI
Service 个background process
Service separated process 执
这 Service UI Activity
个process 执 个 process
执
Android Activity 启动
Service
Android Copyright (c) 2009 Jollen’s Consulting . www.jollen.org/consulting
34. 启动
public class MokoPlayer extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
startService(new Intent ("com.moko.player.START_MUSIC"));
}
}
Android Copyright (c) 2009 Jollen’s Consulting . www.jollen.org/consulting
35. public class MokoService extends Service {
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onStart(Intent intent, int startId) {
}
@Override
public void onDestroy() {
}
}
Android Copyright (c) 2009 Jollen’s Consulting . www.jollen.org/consulting
41. foreground process
visible process
service process
background process
empty process
Android Copyright (c) 2009 Jollen’s Consulting . www.jollen.org/consulting
69. package com.moko.hellomenu;
public final class R {
public static final class attr {
}
public static final class drawable {
public static final int icon=0x7f020000;
}
public static final class id {
public static final int new_message=0x7f060000;
public static final int quit=0x7f060001;
}
public static final class layout {
public static final int main=0x7f030000;
}
public static final class menu {
public static final int options_menu=0x7f050000;
}
public static final class string {
public static final int app_name=0x7f040001;
public static final int hello=0x7f040000;
}
}
Android Copyright (c) 2009 Jollen’s Consulting . www.jollen.org/consulting
73. public boolean onOptionsItemSelected(MenuItem item) {
int item_id = item.getItemId();
switch (item_id){
case R.id.new_message:
Toast.makeText(
this,
"Please enter your message."
+ " Your message is at max 255 characters.",
Toast.LENGTH_LONG).show();
break;
case R.id.quit:
Toast.makeText(
this,
"Going to quit.",
Toast.LENGTH_LONG).show();
break;
default: return false;
}
Android Copyright (c) 2009 Jollen’s Consulting . www.jollen.org/consulting
89. public class MokoPlayer extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
startService(new Intent ("com.moko.player.START_MUSIC"));
}
}
电 丢 android.intent.action.DIAL
Android Copyright (c) 2009 Jollen’s Consulting . www.jollen.org/consulting
97. 实 动 号
public class HelloIntentDialer extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent dial = new Intent();
dial.setAction("android.intent.action.CALL");
dial.setData(Uri.parse("tel:119"));
startActivity(dial);
}
}
Android Copyright (c) 2009 Jollen’s Consulting . www.jollen.org/consulting
123. 实
public class ExampleAppWidgetProvider extends AppWidgetProvider {
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
final int N = appWidgetIds.length;
// Perform this loop procedure for each App Widget that belongs to this provider
for (int i=0; i<N; i++) {
int appWidgetId = appWidgetIds[i];
// Create an Intent to launch ExampleActivity
Intent intent = new Intent(context, ExampleActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
// Get the layout for the App Widget and attach an on-click listener to the button
RemoteViews views = new RemoteViews(context.getPackageName(),
R.layout.appwidget_provider_layout);
views.setOnClickPendingIntent(R.id.button, pendingIntent);
// Tell the AppWidgetManager to perform an update on the current App Widget
appWidgetManager.updateAppWidget(appWidgetId, views);
}
}
}
Android Copyright (c) 2009 Jollen’s Consulting . www.jollen.org/consulting
142. 动画
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2007 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXDelta="0" android:toXDelta="10" android:duration="1000"
android:interpolator="@anim/cycle_7" />
Android Copyright (c) 2009 Jollen’s Consulting . www.jollen.org/consulting
143. <?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2007 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<cycleInterpolator xmlns:android="http://schemas.android.com/apk/res/android"
android:cycles="7" />
Android Copyright (c) 2009 Jollen’s Consulting . www.jollen.org/consulting