zoukankan      html  css  js  c++  java
  • android之service

    MainActivity.Java

     1 package com.example.test;
     2 
     3 import android.app.Activity;
     4 import android.content.Intent;
     5 import android.os.Bundle;
     6 import android.util.Log;
     7 import android.view.View;
     8 import android.view.View.OnClickListener;
     9 import android.widget.Button;
    10 
    11 public class MainActivity extends Activity implements OnClickListener{
    12 
    13     private static final String TAG = "ServicesDemo";
    14       Button buttonStart, buttonStop;
    15 
    16       @Override
    17       public void onCreate(Bundle savedInstanceState) {
    18         super.onCreate(savedInstanceState);
    19         setContentView(R.layout.activity_main);
    20 
    21         buttonStart = (Button) findViewById(R.id.buttonStart);
    22         buttonStop = (Button) findViewById(R.id.buttonStop);
    23 
    24         buttonStart.setOnClickListener(this);
    25         buttonStop.setOnClickListener(this);
    26       }
    27       
    28       public void onClick(View src) {
    29         switch (src.getId()) {
    30         case R.id.buttonStart:
    31           Log.d(TAG, "onClick: starting srvice");
    32           startService(new Intent(this, MyService.class));
    33           break;
    34         case R.id.buttonStop:
    35           Log.d(TAG, "onClick: stopping srvice");
    36           stopService(new Intent(this, MyService.class));
    37           break;
    38         }
    39       }
    40     }

    MyService.Java

     1 package com.example.test;
     2 
     3 import android.app.Service;
     4 import android.content.Intent;
     5 import android.media.MediaPlayer;
     6 import android.os.IBinder;
     7 import android.util.Log;
     8 import android.widget.Toast;
     9 
    10 public class MyService extends Service {
    11         private static final String TAG = "MyService";
    12         MediaPlayer player;
    13        
    14         @Override
    15         public IBinder onBind(Intent intent) {
    16                 return null;
    17         }
    18        
    19         @Override
    20         public void onCreate() {
    21                 Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();
    22                 Log.d(TAG, "onCreate");
    23         }
    24 
    25         @Override
    26         public void onDestroy() {
    27                 Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
    28                 Log.d(TAG, "onDestroy");
    29         }
    30        
    31         @Override
    32         public void onStart(Intent intent, int startid) {
    33                 Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
    34                 Log.d(TAG, "onStart");
    35         }
    36 }

    activity_main.xml

     1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     2     xmlns:tools="http://schemas.android.com/tools"
     3     android:layout_width="match_parent"
     4     android:layout_height="match_parent"
     5     android:paddingBottom="@dimen/activity_vertical_margin"
     6     android:paddingLeft="@dimen/activity_horizontal_margin"
     7     android:paddingRight="@dimen/activity_horizontal_margin"
     8     android:paddingTop="@dimen/activity_vertical_margin"
     9     tools:context=".MainActivity" >
    10 
    11     <Button
    12         android:id="@+id/buttonStart"
    13         android:layout_width="wrap_content"
    14         android:layout_height="wrap_content"
    15         android:layout_alignLeft="@+id/buttonStop"
    16         android:layout_alignParentTop="true"
    17         android:layout_marginTop="74dp"
    18         android:text="开始" />
    19 
    20     <Button
    21         android:id="@+id/buttonStop"
    22         android:layout_width="wrap_content"
    23         android:layout_height="wrap_content"
    24         android:layout_alignParentBottom="true"
    25         android:layout_centerHorizontal="true"
    26         android:layout_marginBottom="122dp"
    27         android:text="结束" />
    28 
    29 </RelativeLayout>

    AndroidManifest.xml,添加

    1 <service android:enabled="true" android:name=".MyService" />
  • 相关阅读:
    SQL 游标示例
    在与SQL Server 建立 连接时出现与网络相关的或特定于实例的错误。未找到或无法访问服务器
    Jquery 设置焦点
    MVC Hidden用法
    Jquery根据name取得所有选中的Checkbox值
    MVC程序部署后页面指向login.aspx
    ref 和out 关键字
    认识关系型数据库
    抽象类和接口详解
    1.穷举法
  • 原文地址:https://www.cnblogs.com/ziyouchutuwenwu/p/3098573.html
Copyright © 2011-2022 走看看