Running Your App (android)

How you run your app depends on two things: whether you have a real Android-powered device and whether you're using Eclipse. This lesson shows you how to install and run your app on a real device and on the Android emulator, and in both cases with either Eclipse or the command line tools.

Before you run your app, you should be aware of a few directories and files in the Android project:

AndroidManifest.xml
The manifest file describes the fundamental characteristics of the app and defines each of its components. You'll learn about various declarations in this file as you read more training classes.
src/
Directory for your app's main source files. By default, it includes an Activity class that runs when your app is launched using the app icon.
res/
Contains several sub-directories for app resources. Here are just a few:
drawable-hdpi/
Directory for drawable objects (such as bitmaps) that are designed for high-density (hdpi) screens. Other drawable directories contain assets designed for other screen densities.
layout/
Directory for files that define your app's user interface.
values/
Directory for other various XML files that contain a collection of resources, such as string and color definitions.

When you build and run the default Android app, the default Activity class starts and loads a layout file that says "Hello World." The result is nothing exciting, but it's important that you understand how to run your app before you start developing.

Run on a Real Device

If you have a real Android-powered device, here's how you can install and run your app:

  1. Plug in your device to your development machine with a USB cable. If you're developing on Windows, you might need to install the appropriate USB driver for your device. For help installing drivers, see the OEM USB Drivers document.
  2. Enable USB debugging on your device.
    • On most devices running Android 3.2 or older, you can find the option under Settings > Applications > Development.
    • On Android 4.0 and newer, it's in Settings > Developer options.

      Note: On Android 4.2 and newer, Developer options is hidden by default. To make it available, go to Settings > About phone and tap Build number seven times. Return to the previous screen to find Developer options.

To run the app from Eclipse, open one of your project's files and click Run from the toolbar. Eclipse installs the app on your connected device and starts it.

Or to run your app from a command line:

  1. Change directories to the root of your Android project and execute:
    ant debug
  2. Make sure the Android SDK platform-tools/ directory is included in your PATH environment variable, then execute:
    adb install bin/MyFirstApp-debug.apk
  3. On your device, locate MyFirstActivity and open it.

That's how you build and run your Android app on a device! To start developing, continue to the next lesson.

Run on the Emulator

Whether you're using Eclipse or the command line, to run your app on the emulator you need to first create an Android Virtual Device (AVD). An AVD is a device configuration for the Android emulator that allows you to model different devices.

Figure 1. The AVD Manager showing a few virtual devices.

To create an AVD:

  1. Launch the Android Virtual Device Manager:
    1. In Eclipse, click Android Virtual Device Manager from the toolbar.
    2. From the command line, change directories to <sdk>/tools/ and execute:
      android avd
  2. In the Android Virtual Device Manager panel, click New.
  3. Fill in the details for the AVD. Give it a name, a platform target, an SD card size, and a skin (HVGA is default).
  4. Click Create AVD.
  5. Select the new AVD from the Android Virtual Device Manager and click Start.
  6. After the emulator boots up, unlock the emulator screen.

To run the app from Eclipse, open one of your project's files and click Run from the toolbar. Eclipse installs the app on your AVD and starts it.

Or to run your app from the command line:

  1. Change directories to the root of your Android project and execute:
    ant debug
  2. Make sure the Android SDK platform-tools/ directory is included in your PATH environment variable, then execute:
    adb install bin/MyFirstApp-debug.apk
  3. On the emulator, locate MyFirstActivity and open it.


为了保持Android应用程序的运行,可以使用以下方法: 1.使用前台服务:前台服务是一种可以在通知栏中显示通知的服务。这样可以让用户知道应用程序正在运行,并且可以随时停止服务。要创建前台服务,请使用startForeground()方法。 ```java public class MyService extends Service { private static final int NOTIFICATION_ID = 1; private static final String CHANNEL_ID = "MyServiceChannel"; @Override public int onStartCommand(Intent intent, int flags, int startId) { createNotificationChannel(); Intent notificationIntent = new Intent(this, MainActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID) .setContentTitle("My Service") .setContentText("Running...") .setSmallIcon(R.drawable.ic_launcher_foreground) .setContentIntent(pendingIntent) .build(); startForeground(NOTIFICATION_ID, notification); // do your work here return START_NOT_STICKY; } @Override public IBinder onBind(Intent intent) { return null; } private void createNotificationChannel() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { NotificationChannel serviceChannel = new NotificationChannel( CHANNEL_ID, "My Service Channel", NotificationManager.IMPORTANCE_DEFAULT ); NotificationManager manager = getSystemService(NotificationManager.class); manager.createNotificationChannel(serviceChannel); } } } ``` 2.使用JobScheduler:JobScheduler是一种系统服务,可以在特定条件下运行作业。例如,当设备处于充电状态时,或者当设备连接到WiFi网络时。要使用JobScheduler,请创建一个JobService类,并在AndroidManifest.xml文件中注册它。 ```java public class MyJobService extends JobService { @Override public boolean onStartJob(JobParameters params) { // do your work here return false; // true if there is still work to do, false otherwise } @Override public boolean onStopJob(JobParameters params) { return true; // true to reschedule the job, false to drop the job } } ``` ```xml <service android:name=".MyJobService" android:permission="android.permission.BIND_JOB_SERVICE" /> ``` 3.使用AlarmManager:AlarmManager是一种系统服务,可以在指定的时间间隔内运行代码。要使用AlarmManager,请创建一个PendingIntent,并使用setRepeating()方法设置重复间隔。 ```java Intent intent = new Intent(this, MyReceiver.class); PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0); AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), AlarmManager.INTERVAL_HALF_HOUR, pendingIntent); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值