如您所知,Android中有飞行模式功能设置。 如果您执行此功能,则无法使用数据,互联网,呼叫,混乱等。如何在应用程序中实现飞行模式功能?
与飞机模式一样。
答案 0 :(得分:2)
请在清单文件中添加权限
boolean isEnabled = Settings.System.getInt(
context.getContentResolver(),
Settings.System.AIRPLANE_MODE_ON, 0) == 1;
以及来自GoJS的代码
检查是否已启用:
// toggle airplane mode
Settings.System.putInt(
context.getContentResolver(),
Settings.System.AIRPLANE_MODE_ON, isEnabled ? 0 : 1);
// Post an intent to reload
Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
intent.putExtra("state", !isEnabled);
sendBroadcast(intent);
要切换它:
IntentFilter intentFilter = new IntentFilter("android.intent.action.SERVICE_STATE");
BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.d("AirplaneMode", "Service state changed");
}
}
context.registerReceiver(receiver, intentFilter);
要获取状态更改通知:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! BuscarCellTableViewCell
if searchController.active {
cell.nombre.text = searchResults[indexPath.row].nombre
cell.marca.text = searchResults[indexPath.row].marca
if let url = NSURL(string: searchResults[indexPath.row].imagen) {
if let data = NSData(contentsOfURL: url) {
cell.imagen.image = UIImage(data: data)
}
}
}
else {
cell.nombre.text = productos[indexPath.row].nombre
cell.marca.text = productos[indexPath.row].marca
if let url = NSURL(string: productos[indexPath.row].imagen) {
if let data = NSData(contentsOfURL: url) {
cell.imagen.image = UIImage(data: data)
}
}
}
return cell
}
override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
// Define the initial state (Before the animation)
cell.alpha = 0.25
// Define the final state (After the animation)
UIView.animateWithDuration(1.0, animations: { cell.alpha = 1 })
}
func getLatestLoans() {
let request = NSURLRequest(URL: NSURL(string: LoadURL)!)
let urlSession = NSURLSession.sharedSession()
let task = urlSession.dataTaskWithRequest(request, completionHandler: { (data, response, error) -> Void in
let res = response as! NSHTTPURLResponse!
var err: NSError?
if error != nil {
println(error.localizedDescription)
}
// Parse JSON data
self.productos = self.parseJsonData(data)
// Reload table view
dispatch_async(dispatch_get_main_queue(), {
self.tableView.reloadData()
})
})
task.resume()
}
func parseJsonData(data: NSData) -> [Producto] {
var productos = [Producto]()
var error:NSError?
let jsonResult = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &error) as? NSDictionary
// Return nil if there is any error
if error != nil {
println(error?.localizedDescription)
}
// Parse JSON data
let jsonProductos = jsonResult?["lista_productos"] as! [AnyObject]
for jsonProducto in jsonProductos {
let producto = Producto()
producto.nombre = jsonProducto["nombre"] as! String
producto.imagen = jsonProducto["imagen"] as! String
productos.append(producto)
}
return productos
}