Android - 应用程序必须 运行 在后台
Android - Application has to run in background
我希望我的应用程序必须 运行 在后台运行。该应用程序主要用于发送基于 IBeacons 的推送通知,即 Apple 提供的低功耗蓝牙技术。
这很好我的应用程序能够收到通知,但它应该处于打开模式(应用程序应该打开)。但我希望我的应用程序必须在后台 运行 就像每当用户进入 IBeacon 接近范围时它在内部必须收到通知一样。
主类:
public class MainActivity extends Activity {
private static final String ESTIMOTE_PROXIMITY_UUID = "B9407F30-F5F8-466E-AFF9-25556B57FE6D";
private static final Region ALL_ESTIMOTE_BEACONS = new Region("regionId",
ESTIMOTE_PROXIMITY_UUID, null, null);
protected static final String TAG = "EstimoteiBeacon";
BeaconManager beaconManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView tx = (TextView) findViewById(R.id.textView1);
final List<Integer> test1 = new ArrayList<Integer>();
beaconManager = new BeaconManager(this);
beaconManager.connect(new BeaconManager.ServiceReadyCallback() {
@Override
public void onServiceReady() {
final ArrayList AList = new ArrayList();
try {
beaconManager.startRanging(ALL_ESTIMOTE_BEACONS);
beaconManager
.setRangingListener(new BeaconManager.RangingListener() {
@Override
public void onBeaconsDiscovered(Region arg0,
List<Beacon> beacons) {
for (Beacon beacon : beacons) {
int major = beacon.getMajor();
test1.add(major);
AList.add(major);
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putIntegerArrayListExtra("test", (ArrayList<Integer>) test1);
tx.setText(String.valueOf(major));
startActivity(intent);
}
}
});
} catch (RemoteException e) {
Log.e("error", "Cannot start ranging", e);
}
}
});
}
// ---stop ranging for beacons when activity is killed---
@Override
protected void onStop() {
super.onStop();
try {
beaconManager.stopRanging(ALL_ESTIMOTE_BEACONS);
} catch (RemoteException e) {
Log.e
(TAG, "Cannot stop", e);
}
}}
二等
public class SecondActivity extends Activity {
String name,str;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
//Bundle extras = getIntent().getExtras();
TextView tv1 = (TextView) findViewById(R.id.textView1);
TextView tv2 = (TextView) findViewById(R.id.textView2);
TextView tv3 = (TextView) findViewById(R.id.textView3);
Intent intent=getIntent();
ArrayList<Integer> test = intent.getIntegerArrayListExtra("test");
tv1.setText(test.get(0).toString());
tv2.setText(test.get(1).toString());
tv3.setText(test.get(2).toString());
String loginRequest = "http://172.17.13.10:8080/RESTfulDemoDerby/webresources/com.mss.mmxregistration/Message?maxvalue=32623";
Log.e("retalier url", loginRequest);
HttpGet request = new HttpGet(loginRequest);
Bluetooth loginTask = new Bluetooth(
SecondActivity.this, request);
loginTask.execute();
}
class Bluetooth extends Bluetoothtask {
public Bluetooth(Context context, HttpRequestBase request) {
super(context, request);
// TODO Auto-generated constructor stub
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
if (result != null) {
DocumentBuilderFactory factory = DocumentBuilderFactory
.newInstance();
DocumentBuilder builder;
InputSource is;
try {
factory.setNamespaceAware(true);
builder = factory.newDocumentBuilder();
is = new InputSource(new StringReader(result));
Document doc = builder.parse(is);
NodeList list = doc.getElementsByTagName("Payload");
result = list.item(0).getTextContent();
} catch (ParserConfigurationException e) {
} catch (SAXException e) {
} catch (IOException e) {
}
JSONObject jsonObj;
Gson gson = new Gson();
try {
jsonObj = new JSONObject(result);
Log.e("Result", result);
JSONObject jsonResponse;
try {
jsonResponse = new JSONObject(result);
JSONArray cast = jsonResponse.getJSONArray("result");
for (int i=0; i<cast.length(); i++) {
JSONObject actor = cast.getJSONObject(i);
name = actor.getString("message");
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Context context = getApplicationContext();
CharSequence text = "TURNING_ON BLUETOOTH";
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, text, 15);
toast.show();
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
@SuppressWarnings("deprecation")
Notification notification = new Notification(R.drawable.ic_launcher,
"New Message", System.currentTimeMillis());
Intent notificationIntent = new Intent(SecondActivity.this, SecondActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(SecondActivity.this, 0,
notificationIntent, 0);
notification.setLatestEventInfo(SecondActivity.this, "Title",
name, pendingIntent);
notificationManager.notify(9999, notification);
} catch (Exception e) {
}
}
}
}@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
我们可以为后台应用程序使用广播接收器 运行ning 但为此我们将如何实现此示例。
不幸的是,您围绕 Asynctasks 和 Activity 构建的所有内容都不符合您的目标。你想查找 services.
将逻辑转移到服务后,您将希望 start/stop 该服务基于广播接收器或持久通知。然后你的 Activity 可以绑定到你的服务,如果你愿意,可以从他们那里获取更多信息,或者你可以使用其他形式的服务->activity 通信,如广播。
请注意,这可能会消耗电池电量,因此您需要为用户提供关闭它的功能,或者至少选择 in/out。
我希望我的应用程序必须 运行 在后台运行。该应用程序主要用于发送基于 IBeacons 的推送通知,即 Apple 提供的低功耗蓝牙技术。 这很好我的应用程序能够收到通知,但它应该处于打开模式(应用程序应该打开)。但我希望我的应用程序必须在后台 运行 就像每当用户进入 IBeacon 接近范围时它在内部必须收到通知一样。
主类:
public class MainActivity extends Activity {
private static final String ESTIMOTE_PROXIMITY_UUID = "B9407F30-F5F8-466E-AFF9-25556B57FE6D";
private static final Region ALL_ESTIMOTE_BEACONS = new Region("regionId",
ESTIMOTE_PROXIMITY_UUID, null, null);
protected static final String TAG = "EstimoteiBeacon";
BeaconManager beaconManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView tx = (TextView) findViewById(R.id.textView1);
final List<Integer> test1 = new ArrayList<Integer>();
beaconManager = new BeaconManager(this);
beaconManager.connect(new BeaconManager.ServiceReadyCallback() {
@Override
public void onServiceReady() {
final ArrayList AList = new ArrayList();
try {
beaconManager.startRanging(ALL_ESTIMOTE_BEACONS);
beaconManager
.setRangingListener(new BeaconManager.RangingListener() {
@Override
public void onBeaconsDiscovered(Region arg0,
List<Beacon> beacons) {
for (Beacon beacon : beacons) {
int major = beacon.getMajor();
test1.add(major);
AList.add(major);
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putIntegerArrayListExtra("test", (ArrayList<Integer>) test1);
tx.setText(String.valueOf(major));
startActivity(intent);
}
}
});
} catch (RemoteException e) {
Log.e("error", "Cannot start ranging", e);
}
}
});
}
// ---stop ranging for beacons when activity is killed---
@Override
protected void onStop() {
super.onStop();
try {
beaconManager.stopRanging(ALL_ESTIMOTE_BEACONS);
} catch (RemoteException e) {
Log.e
(TAG, "Cannot stop", e);
}
}}
二等
public class SecondActivity extends Activity {
String name,str;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
//Bundle extras = getIntent().getExtras();
TextView tv1 = (TextView) findViewById(R.id.textView1);
TextView tv2 = (TextView) findViewById(R.id.textView2);
TextView tv3 = (TextView) findViewById(R.id.textView3);
Intent intent=getIntent();
ArrayList<Integer> test = intent.getIntegerArrayListExtra("test");
tv1.setText(test.get(0).toString());
tv2.setText(test.get(1).toString());
tv3.setText(test.get(2).toString());
String loginRequest = "http://172.17.13.10:8080/RESTfulDemoDerby/webresources/com.mss.mmxregistration/Message?maxvalue=32623";
Log.e("retalier url", loginRequest);
HttpGet request = new HttpGet(loginRequest);
Bluetooth loginTask = new Bluetooth(
SecondActivity.this, request);
loginTask.execute();
}
class Bluetooth extends Bluetoothtask {
public Bluetooth(Context context, HttpRequestBase request) {
super(context, request);
// TODO Auto-generated constructor stub
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
if (result != null) {
DocumentBuilderFactory factory = DocumentBuilderFactory
.newInstance();
DocumentBuilder builder;
InputSource is;
try {
factory.setNamespaceAware(true);
builder = factory.newDocumentBuilder();
is = new InputSource(new StringReader(result));
Document doc = builder.parse(is);
NodeList list = doc.getElementsByTagName("Payload");
result = list.item(0).getTextContent();
} catch (ParserConfigurationException e) {
} catch (SAXException e) {
} catch (IOException e) {
}
JSONObject jsonObj;
Gson gson = new Gson();
try {
jsonObj = new JSONObject(result);
Log.e("Result", result);
JSONObject jsonResponse;
try {
jsonResponse = new JSONObject(result);
JSONArray cast = jsonResponse.getJSONArray("result");
for (int i=0; i<cast.length(); i++) {
JSONObject actor = cast.getJSONObject(i);
name = actor.getString("message");
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Context context = getApplicationContext();
CharSequence text = "TURNING_ON BLUETOOTH";
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, text, 15);
toast.show();
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
@SuppressWarnings("deprecation")
Notification notification = new Notification(R.drawable.ic_launcher,
"New Message", System.currentTimeMillis());
Intent notificationIntent = new Intent(SecondActivity.this, SecondActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(SecondActivity.this, 0,
notificationIntent, 0);
notification.setLatestEventInfo(SecondActivity.this, "Title",
name, pendingIntent);
notificationManager.notify(9999, notification);
} catch (Exception e) {
}
}
}
}@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
我们可以为后台应用程序使用广播接收器 运行ning 但为此我们将如何实现此示例。
不幸的是,您围绕 Asynctasks 和 Activity 构建的所有内容都不符合您的目标。你想查找 services.
将逻辑转移到服务后,您将希望 start/stop 该服务基于广播接收器或持久通知。然后你的 Activity 可以绑定到你的服务,如果你愿意,可以从他们那里获取更多信息,或者你可以使用其他形式的服务->activity 通信,如广播。
请注意,这可能会消耗电池电量,因此您需要为用户提供关闭它的功能,或者至少选择 in/out。