作者:sundroid
个人站点:sundroid.cn 邮箱: hfutsnjc@163.com 微博:http://weibo.com/Sundroid
android系统提供了一些访问系统自带的一些服务,比如通知、搜索、地理位置等,我想大概可能会注意到的就是调用这个方法时的步骤基本上是差不多的,一般都是
XXXManager xxxManager = (XXXManager)getSystemService(XXX_SERVICE);
xxxManager 调用一些方法,google的工程师在设计这些方法时遵循的逻辑基本上是一致的。下面就简单介绍几个Manager,然后其他的使用方法没多少区别。
- NotificationManager
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
private Button notificationBtn;
private NotificationManager notificationManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
notificationBtn = (Button) findViewById(R.id.notificationBtn);
//获取NotificationManager对象
notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//创建Notification对象
Notification notification = new Notification();
notification.icon = R.drawable.abc_btn_radio_material;
notification.tickerText = "Notification demo";
PendingIntent intent = PendingIntent.getActivity(MainActivity.this,0,new Intent(MainActivity.this,MainActivity.class),0);
//设置通知信息
notification.setLatestEventInfo(MainActivity.this,"Notification","Content of Notification Demo",intent);
//执行通知
notificationManager.notify(0,notification);
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
//取消通知
notificationManager.cancel(0);
}
}
运行效果
- LocationManager
import android.app.Activity;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
private Button locationBtn;
private LocationManager locationManager;
private final LocationListener locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
if (location != null) {
Log.i("SuperMap", "Location changed : Lat: "
+ location.getLatitude() + " Lng: "
+ location.getLongitude());
}
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// Provider被disable时触发此函数,比如GPS被关闭
}
@Override
public void onProviderEnabled(String provider) {
// Provider被enable时触发此函数,比如GPS被打开
}
@Override
public void onProviderDisabled(String provider) {
// Provider的转态在可用、暂时不可用和无服务三个状态直接切换时触发此函数
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
locationBtn = (Button) findViewById(R.id.locationBtn);
locationBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,1000,0,locationListener);
}
});
}
}
需要加上访问位置的权限。
<permission android:name="android.permission.ACCESS_FINE_LOCATION"/>