问题描述
我添加下面我MainActivity,应用程序从数据库中获取数据,并自动刷新和向下滑动。
我的问题是,如何在地球上可以通过声音和振动通知关于新的牵强插入用户?
的要对新刀片的定义更为具体,该应用程序在它0数据开始,一度刷新一个PHP调用从数据库中获取新的JSON字符串,是德codeD中应用程序,并显示在列表视图。的
公共类MainActivity扩展AppCompatActivity实现SwipeRefreshLayout.OnRefreshListener { 私人INT mInterval = 5000; //缺省为5秒,可在以后更改
私人处理器mHandler; 私有String TAG = MainActivity.class.getSimpleName(); 私人字符串URL =http://10.0.0.2:0080/stringtest2.php?offset=;
私人SwipeRefreshLayout swipeRefreshLayout;
私人的ListView ListView的;
私人SwipeListAdapter适配器;
私人列表<排序> orderList; //开始偏移量将为0,以后在解析JSON更新
私人INT偏移= 0; @覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main); ListView控件=(ListView控件)findViewById(R.id.listView);
//RelativeLayout.LayoutParams LAYOUT_DESCRIPTION =新RelativeLayout.LayoutParams(50,10); //Rl.setLayoutParams(layout_description); swipeRefreshLayout =(SwipeRefreshLayout)findViewById(R.id.swipe_refresh_layout); orderList =新的ArrayList<>();
适配器=新SwipeListAdapter(这一点,orderList);
listView.setAdapter(适配器); swipeRefreshLayout.setOnRefreshListener(本); / **
*上的活动显示刷卡刷新动画制作
*由于动画将不启动的onCreate,可运行后使用
* /
swipeRefreshLayout.post(新的Runnable(){
@覆盖
公共无效的run(){
swipeRefreshLayout.setRefreshing(真); fetchOrders();
}
}
); mHandler =新的处理程序();
startRepeatingTask();
} / **
*当刷卡刷新拉低这种方法被称为
* / 可运行mStatusChecker =新的Runnable(){
@覆盖
公共无效的run(){
//更新状态(); //这个功能可以改变mInterval的价值。
mHandler.postDelayed(mStatusChecker,mInterval);
}
};
无效startRepeatingTask(){
mStatusChecker.run();
} 无效申通快递prepeatingTask(){
mHandler.removeCallbacks(mStatusChecker);
} //添加code从这里开始
可运行mAutoRefreshRunnable =新的Runnable(){
@覆盖
公共无效的run(){
fetchOrders();
mHandler.postDelayed(mAutoRefreshRunnable,60000);
}
}; @覆盖
保护无效onResume(){
super.onResume();
mHandler.postDelayed(mAutoRefreshRunnable,60000);
} @覆盖
保护无效的onPause(){
super.onPause();
mHandler.removeCallbacks(mAutoRefreshRunnable);
}
//添加code到此为止
@覆盖
公共无效onRefresh(){
fetchOrders();
} / **
*通过HTTP调用抓取电影JSON
* /
私人无效fetchOrders(){ //显示发出HTTP调用之前刷新动画
swipeRefreshLayout.setRefreshing(真); //附加偏移网址
字符串的URL = URL +偏移量; //排球的JSON数组请求对象
JsonArrayRequest REQ =新JsonArrayRequest(URL,
新Response.Listener< JSONArray>(){
@覆盖
公共无效onResponse(JSONArray响应){
Log.d(TAG,response.toString()); 如果(response.length()大于0){ //通过JSON循环,并增加订单列表
的for(int i = 0; I< response.length();我++){
尝试{
JSONObject的orderObj = response.getJSONObject(I) INT等级= orderObj.getInt(等级);
字符串title = orderObj.getString(标题); 令M =新订单(等级,职称); orderList.add(0,M); //更新偏移值来最高值
如果(排名> = OFFSET)
偏移量=排名; }赶上(JSONException E){
Log.e(TAG,JSON解析错误:+ e.getMessage());
}
} adapter.notifyDataSetChanged();
} //停止刷卡刷新
swipeRefreshLayout.setRefreshing(假); }
},新Response.ErrorListener(){
@覆盖
公共无效onErrorResponse(VolleyError错误){
Log.e(TAG,服务器错误:+ error.getMessage()); Toast.makeText(getApplicationContext(),无法连接到数据库,Toast.LENGTH_LONG).show(); //停止刷卡刷新
swipeRefreshLayout.setRefreshing(假);
}
}); //添加请求,请求队列
MyApplication.getInstance()addToRequestQueue(REQ)。
}
}
在你获取新的数据,所以你需要调用 notifyDataSetChanged()
来让一个地方列表视图更新其内容。
正确的电话后,就可以使用振动器开始振动
振动器V =(振动器)getSystemService(Context.VIBRATOR_SERVICE);
//为震动500毫秒
v.vibrate(500);
请参阅:
要播放的声音,你需要一个MediaPlayer的:
MediaPlayer的MP = MediaPlayer.create(这一点,yourSoundFile);
mp.start();
I've added my MainActivity below, the application fetches data from a database and refreshes automatically and on swipe down.
My question is, how on earth can it notify the user about "new" fetched inserts via sound and vibration?
To be more specific regarding the definition of "new inserts", the application starts with 0 data in it, once refreshed a php call from the database gets a new JSON string and is decoded in the app and appears on the listview.
public class MainActivity extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener {
private int mInterval = 5000; // 5 seconds by default, can be changed later
private Handler mHandler;
private String TAG = MainActivity.class.getSimpleName();
private String URL = "http://10.0.0.2:0080/stringtest2.php?offset=";
private SwipeRefreshLayout swipeRefreshLayout;
private ListView listView;
private SwipeListAdapter adapter;
private List<Order> orderList;
// initially offset will be 0, later will be updated while parsing the json
private int offSet = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.listView);
//RelativeLayout.LayoutParams layout_description = new RelativeLayout.LayoutParams(50,10);
//Rl.setLayoutParams(layout_description);
swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_refresh_layout);
orderList = new ArrayList<>();
adapter = new SwipeListAdapter(this, orderList);
listView.setAdapter(adapter);
swipeRefreshLayout.setOnRefreshListener(this);
/**
* Showing Swipe Refresh animation on activity create
* As animation won't start on onCreate, post runnable is used
*/
swipeRefreshLayout.post(new Runnable() {
@Override
public void run() {
swipeRefreshLayout.setRefreshing(true);
fetchOrders();
}
}
);
mHandler = new Handler();
startRepeatingTask();
}
/**
* This method is called when swipe refresh is pulled down
*/
Runnable mStatusChecker = new Runnable() {
@Override
public void run() {
//updateStatus(); //this function can change value of mInterval.
mHandler.postDelayed(mStatusChecker, mInterval);
}
};
void startRepeatingTask() {
mStatusChecker.run();
}
void stopRepeatingTask() {
mHandler.removeCallbacks(mStatusChecker);
}
//added code start here
Runnable mAutoRefreshRunnable = new Runnable() {
@Override
public void run() {
fetchOrders();
mHandler.postDelayed(mAutoRefreshRunnable, 60000);
}
};
@Override
protected void onResume() {
super.onResume();
mHandler.postDelayed(mAutoRefreshRunnable, 60000);
}
@Override
protected void onPause(){
super.onPause();
mHandler.removeCallbacks(mAutoRefreshRunnable);
}
//added code ends here
@Override
public void onRefresh() {
fetchOrders();
}
/**
* Fetching movies json by making http call
*/
private void fetchOrders() {
// showing refresh animation before making http call
swipeRefreshLayout.setRefreshing(true);
// appending offset to url
String url = URL + offSet;
// Volley's json array request object
JsonArrayRequest req = new JsonArrayRequest(url,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.d(TAG, response.toString());
if (response.length() > 0) {
// looping through json and adding to order list
for (int i = 0; i < response.length(); i++) {
try {
JSONObject orderObj = response.getJSONObject(i);
int rank = orderObj.getInt("rank");
String title = orderObj.getString("title");
Order m = new Order(rank, title);
orderList.add(0, m);
// updating offset value to highest value
if (rank >= offSet)
offSet = rank;
} catch (JSONException e) {
Log.e(TAG, "JSON Parsing error: " + e.getMessage());
}
}
adapter.notifyDataSetChanged();
}
// stopping swipe refresh
swipeRefreshLayout.setRefreshing(false);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, "Server Error: " + error.getMessage());
Toast.makeText(getApplicationContext(), "Can't connect to database", Toast.LENGTH_LONG).show();
// stopping swipe refresh
swipeRefreshLayout.setRefreshing(false);
}
});
// Adding request to request queue
MyApplication.getInstance().addToRequestQueue(req);
}
}
After you fetch new data, there is a place you need to call notifyDataSetChanged()
to let the listview update its content.
right after that call, you can use Vibrator to start a vibration
Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
// Vibrate for 500 milliseconds
v.vibrate(500);
see: http://developer.android.com/reference/android/os/Vibrator.html
To play sound, you need a MediaPlayer:
MediaPlayer mp = MediaPlayer.create(this, yourSoundFile);
mp.start();
这篇关于ListView控件:如何通知有关与声音和振动新获取数据的用户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!