当前位置:  编程技术>移动开发
本页文章导读:
    ▪获得正在运行的Activity        取得正在运行的Activity     在main.xml中:   <?xml version="1.0" encoding="utf-8"?> <LinearLayout   xmlns:android="http://schemas.android.com/apk/res/android"   android:orientation="vertical"   android:layout_width="fill_pare.........
    ▪ 获得正在运行的服务        取得正在运行的服务     在main.xml中:   <?xml version="1.0" encoding="utf-8"?> <LinearLayout   xmlns:android="http://schemas.android.com/apk/res/android"   android:orientation="vertical"   android:layout_width="fill_parent.........
    ▪ 获得手机的网络信息       取得手机的网络信息     在main.xml中:   <?xml version="1.0" encoding="utf-8"?> <LinearLayout   xmlns:android="http://schemas.android.com/apk/res/android"   android:orientation="vertical"   android:layout_width="fill_parent.........

[1]获得正在运行的Activity
    来源: 互联网  发布时间: 2014-02-18
取得正在运行的Activity



 

 

在main.xml中:

 

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout

  xmlns:android="http://schemas.android.com/apk/res/android"

  android:orientation="vertical"

  android:layout_width="fill_parent"

  android:layout_height="fill_parent"

  android:background="#3399ff">

  <ListView

     android:id="@+id/tasklist"

     android:layout_gravity="center_horizontal"

     android:layout_width="fill_parent"

     android:layout_height="wrap_content" />

</LinearLayout>

 

 

 

 

 

在MyActivityRun.java中:

 

package com.li.activityrun;

 

import java.util.ArrayList;

import java.util.Iterator;

import java.util.List;

 

import android.app.Activity;

import android.app.ActivityManager;

import android.content.Context;

import android.os.Bundle;

import android.widget.ArrayAdapter;

import android.widget.ListAdapter;

import android.widget.ListView;

 

public class MyActivityRun extends Activity {

  private ListView tasklist = null ;

  private ListAdapter adapter = null ;

  private List<String> all = new ArrayList<String>() ;

  private ActivityManager activityManager = null ;

  private List<ActivityManager.RunningTaskInfo> allTaskInfo ;

  @Override

  public void onCreate(Bundle savedInstanceState) {

     super.onCreate(savedInstanceState);

     super.setContentView(R.layout.main);

     this.tasklist = (ListView) super.findViewById(R.id.tasklist) ;

     this.activityManager = (ActivityManager) super

         .getSystemService(Context.ACTIVITY_SERVICE);

     this.listActivity() ;

  }

 

  private void listActivity() {

     this.allTaskInfo = this.activityManager.getRunningTasks(30); // 返回30条

     Iterator<ActivityManager.RunningTaskInfo> iterInfo = this.allTaskInfo.iterator() ;

     while(iterInfo.hasNext()) {

       ActivityManager.RunningTaskInfo task = iterInfo.next() ;

       this.all.add("【ID = " + task.id + " 】 "

            + task.baseActivity.getClassName());

     }

     this.adapter = new ArrayAdapter<String>(this,

         android.R.layout.simple_list_item_1, this.all);

     this.tasklist.setAdapter(this.adapter) ;

  }

}

 

 

 

 

 

 

在AndroidManifest.xml中修改权限:

 

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

    package="com.li.activityrun"

    android:versionCode="1"

    android:versionName="1.0" >

 

    <uses-sdk

        android:minSdkVersion="8"

        android:targetSdkVersion="15" />

    <uses-permission android:name="android.permission.GET_TASKS"/>

 

    <application

        android:icon="@drawable/ic_launcher"

        android:label="@string/app_name"

        android:theme="@style/AppTheme" >

        <activity

            android:name=".MyActivityRun"

            android:label="@string/title_activity_my_activity_run" >

            <intent-filter>

                <action android:name="android.intent.action.MAIN" />

 

                <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>

        </activity>

    </application>

 

</manifest>

 

 


    
[2] 获得正在运行的服务
    来源: 互联网  发布时间: 2014-02-18
取得正在运行的服务



 

 

在main.xml中:

 

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout

  xmlns:android="http://schemas.android.com/apk/res/android"

  android:orientation="vertical"

  android:layout_width="fill_parent"

  android:layout_height="fill_parent"

  android:background="#3399ff">

  <ListView

     android:id="@+id/tasklist"

     android:layout_gravity="center_horizontal"

     android:layout_width="fill_parent"

     android:layout_height="wrap_content" />

</LinearLayout>

 

 

 

 

 

在MyActivityRun.java中:

 

package com.li.activityrun;

 

import java.util.ArrayList;

import java.util.Iterator;

import java.util.List;

 

import android.app.Activity;

import android.app.ActivityManager;

import android.content.Context;

import android.os.Bundle;

import android.widget.ArrayAdapter;

import android.widget.ListAdapter;

import android.widget.ListView;

 

public class MyActivityRun extends Activity {

  private ListView tasklist = null ;

  private ListAdapter adapter = null ;

  private List<String> all = new ArrayList<String>() ;

  private ActivityManager activityManager = null ;

  private List<ActivityManager.RunningServiceInfo> allTaskInfo ;

  @Override

  public void onCreate(Bundle savedInstanceState) {

     super.onCreate(savedInstanceState);

     super.setContentView(R.layout.main);

     this.tasklist = (ListView) super.findViewById(R.id.tasklist) ;

     this.activityManager = (ActivityManager) super

         .getSystemService(Context.ACTIVITY_SERVICE);

     this.listActivity() ;

  }

 

  private void listActivity() {

    this.allTaskInfo = this.activityManager.getRunningServices(30) ;

     Iterator<ActivityManager.RunningServiceInfo> iterInfo = this.allTaskInfo.iterator() ;

     while(iterInfo.hasNext()) {

       ActivityManager.RunningServiceInfo service = iterInfo.next() ;

       this.all.add("【ID = " + service.pid + " 】 "

            + service.process);

     }

     this.adapter = new ArrayAdapter<String>(this,

         android.R.layout.simple_list_item_1, this.all);

     this.tasklist.setAdapter(this.adapter) ;

  }

}

 

 

 

在AndroidManifest.xml中修改权限:

 

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

    package="com.li.activityrun"

    android:versionCode="1"

    android:versionName="1.0" >

 

    <uses-sdk

        android:minSdkVersion="8"

        android:targetSdkVersion="15" />

    <uses-permission android:name="android.permission.GET_TASKS"/>

 

    <application

        android:icon="@drawable/ic_launcher"

        android:label="@string/app_name"

        android:theme="@style/AppTheme" >

        <activity

            android:name=".MyActivityRun"

            android:label="@string/title_activity_my_activity_run" >

            <intent-filter>

                <action android:name="android.intent.action.MAIN" />

 

                <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>

        </activity>

    </application>

 

</manifest>

 

 


    
[3] 获得手机的网络信息
    来源: 互联网  发布时间: 2014-02-18
取得手机的网络信息



 

 

在main.xml中:

 

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout

  xmlns:android="http://schemas.android.com/apk/res/android"

  android:orientation="vertical"

  android:layout_width="fill_parent"

  android:layout_height="fill_parent"

  android:background="#3399ff">

  <ListView

      android:layout_marginLeft="8dp"

      android:layout_marginRight="8dp"

     android:id="@+id/infolist"

     android:layout_width="fill_parent"

     android:layout_height="wrap_content" />

</LinearLayout>

 

 

 

 

 

 

 

在MyTelephoneManagerDemo.java中:

 

package com.li.telephonemanager;

 

import java.util.ArrayList;

import java.util.List;

 

import android.app.Activity;

import android.content.Context;

import android.os.Bundle;

import android.telephony.TelephonyManager;

import android.widget.ArrayAdapter;

import android.widget.ListAdapter;

import android.widget.ListView;

 

public class MyTelephoneManagerDemo extends Activity {

  private ListView infolist = null ;

  private ListAdapter adapter = null ;

  private List<String> all = new ArrayList<String>() ;

  private TelephonyManager manager = null ;

  @Override

  public void onCreate(Bundle savedInstanceState) {

     super.onCreate(savedInstanceState);

     super.setContentView(R.layout.main);

     this.infolist = (ListView) super.findViewById(R.id.infolist) ;

     this.manager = (TelephonyManager) super

         .getSystemService(Context.TELEPHONY_SERVICE); // 电话服务

     this.list() ;

  }

  private void list(){ // 列出一些信息

     this.all.add(this.manager.getLine1Number() == null ? "没有手机号码" : "手机号码:"

         + this.manager.getLine1Number());

    this.all.add(this.manager.getNetworkOperatorName() == null ? "没有移动服务商"

         : "移动服务商:" + this.manager.getNetworkOperatorName());

     if (this.manager.getPhoneType() == TelephonyManager.NETWORK_TYPE_CDMA) {

       this.all.add("移动网络:CDMA");

     } else if (this.manager.getPhoneType() == TelephonyManager.NETWORK_TYPE_GPRS) {

       this.all.add("移动网络:GPRS");

     } else {

       this.all.add("移动网络:未知");

     }

     if (this.manager.getNetworkType() == TelephonyManager.PHONE_TYPE_GSM) {

       this.all.add("网络类型:GSM");

     } else if (this.manager.getNetworkType() == TelephonyManager.PHONE_TYPE_CDMA) {

       this.all.add("网络类型:CDMA");

     } else {

       this.all.add("网络类型:未知");

     }

    this.all.add("是否漫游:" + (this.manager.isNetworkRoaming() ? "漫游" : "非漫游"));

     this.adapter = new ArrayAdapter<String>(this,

         android.R.layout.simple_list_item_1, this.all);

     this.infolist.setAdapter(this.adapter) ;

  }

}

 

 

 

 

 

 

在AndroidManifest.xml中修改权限:

 

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

    package="com.li.telephonemanager"

    android:versionCode="1"

    android:versionName="1.0" >

 

    <uses-sdk

        android:minSdkVersion="8"

        android:targetSdkVersion="15" />

  <uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>

    <application

        android:icon="@drawable/ic_launcher"

        android:label="@string/app_name"

        android:theme="@style/AppTheme" >

        <activity

            android:name=".MyTelephoneManagerDemo"

            android:label="@string/title_activity_my_telephone_manager_demo" >

            <intent-filter>

                <action android:name="android.intent.action.MAIN" />

 

                <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>

        </activity>

    </application>

 

</manifest>

 


    
最新技术文章:
▪Android开发之登录验证实例教程
▪Android开发之注册登录方法示例
▪Android获取手机SIM卡运营商信息的方法
▪Android实现将已发送的短信写入短信数据库的...
▪Android发送短信功能代码
▪Android根据电话号码获得联系人头像实例代码
▪Android中GPS定位的用法实例
▪Android实现退出时关闭所有Activity的方法
▪Android实现文件的分割和组装
▪Android录音应用实例教程
▪Android双击返回键退出程序的实现方法
▪Android实现侦听电池状态显示、电量及充电动...
▪Android获取当前已连接的wifi信号强度的方法
▪Android实现动态显示或隐藏密码输入框的内容
▪根据USER-AGENT判断手机类型并跳转到相应的app...
▪Android Touch事件分发过程详解
▪Android中实现为TextView添加多个可点击的文本
▪Android程序设计之AIDL实例详解
▪Android显式启动与隐式启动Activity的区别介绍
▪Android按钮单击事件的四种常用写法总结
▪Android消息处理机制Looper和Handler详解
▪Android实现Back功能代码片段总结
▪Android实用的代码片段 常用代码总结
▪Android实现弹出键盘的方法
▪Android中通过view方式获取当前Activity的屏幕截...
▪Android提高之自定义Menu(TabMenu)实现方法
▪Android提高之多方向抽屉实现方法
▪Android提高之MediaPlayer播放网络音频的实现方法...
▪Android提高之MediaPlayer播放网络视频的实现方法...
▪Android提高之手游转电视游戏的模拟操控
 


站内导航:


特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!

©2012-2021,,E-mail:www_#163.com(请将#改为@)

浙ICP备11055608号-3