我有一个Android应用程序,我想从服务显示通知或吐司每10秒例如,当应用程序被关闭或完成如何可以显示从服务敬酒每10秒
回答
我有一个样品活性,服务等级和Timer类提供如下。在应用程序中使用类似的实现。
活动课
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
public class Sample extends Activity {
Button button1,button2;
private Handler mHandler = new Handler();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Call the start and stop method when needed.
}
public void Start(View v)
{
startService(new Intent(MainActivity.this , Sample_service.class));
}
public void Stop(View v)
{
stopService(new Intent(MainActivity.this , Sample_service.class));
}
}
服务类
package com.example.connect;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;
public class Sample_service extends Service{
Timer timer = new Timer();
TimerTask updateProfile = new CustomTimerTask(Sample_service.this);
public void onCreate() {
super.onCreate();
Toast.makeText(this, "Service Started", Toast.LENGTH_SHORT).show();
timer.scheduleAtFixedRate(updateProfile, 0, 10000);
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Toast.makeText(this, "Service Stopped", Toast.LENGTH_SHORT).show();
timer.cancel();
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
}
Timer类
package com.example.connect;
import java.util.TimerTask;
import android.content.Context;
import android.os.Handler;
import android.widget.Toast;
public class CustomTimerTask extends TimerTask {
private Context context;
private Handler mHandler = new Handler();
public CustomTimerTask(Context con) {
this.context = con;
}
@Override
public void run() {
new Thread(new Runnable() {
public void run() {
mHandler.post(new Runnable() {
public void run() {
Toast.makeText(context, "DISPLAY YOUR MESSAGE", Toast.LENGTH_SHORT).show();
}
});
}
}).start();
}
}
什么是“Connect_service”上的“Sample_service”类? – user3404171 2014-11-06 14:32:16
从技术上讲,当任务执行你在onCreate()方法中写的,它会自动关闭。为此,您可以使用定时器/调度程序。
实施例:
Timer timer;
TimerTask timerTask;
timer.schedule(timerTask, 5000, 10000);
timerTask = new TimerTask() {
public void run() {
//use a handler to run a toast that shows the current timestamp
handler.post(new Runnable() {
public void run() {
Toast toast = Toast.makeText(getApplicationContext(), strDate, duration);
toast.show();
}
});
}
};
我该如何使用此代码? – user3404171 2014-11-06 13:59:42
您可以在任何地方使用此代码,在活动或服务类onCreate()中。 – samsad 2014-11-06 14:01:21
在服务类onCreate()中使用这段代码。它会工作:) – samsad 2014-11-06 14:02:15
不要忘了在清单文件中添加应用程序标签中为您服务:
<service android:name=".ServiceGPS"
android:permission="[Add permission here if exists]"
android:label="[service name]" android:exported="true"
android:enabled="true">
</service>
我在这里给出了三个文件,MainActivity,Manifest和MyService文件在你的应用程序中实现它,它将显示每10秒的吐司服务。
MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);//load the layout file
startService(new Intent(this,MyService.class));//use to start the services
}
}
MyService.java
public class MyService extends Service {
public static final long INTERVAL=10000;//variable to execute services every 10 second
private Handler mHandler=new Handler(); // run on another Thread to avoid crash
private Timer mTimer=null; // timer handling
@Nullable
@Override
public IBinder onBind(Intent intent) {
throw new UnsupportedOperationException("unsupported Operation");
}
@Override
public void onCreate() {
// cancel if service is already existed
if(mTimer!=null)
mTimer.cancel();
else
mTimer=new Timer(); // recreate new timer
mTimer.scheduleAtFixedRate(new TimeDisplayTimerTask(),0,INTERVAL);// schedule task
}
@Override
public void onDestroy() {
Toast.makeText(this, "In Destroy", Toast.LENGTH_SHORT).show();//display toast when method called
mTimer.cancel();//cancel the timer
}
//inner class of TimeDisplayTimerTask
private class TimeDisplayTimerTask extends TimerTask {
@Override
public void run() {
// run on another thread
mHandler.post(new Runnable() {
@Override
public void run() {
// display toast at every 10 second
Toast.makeText(getApplicationContext(), "Notify", Toast.LENGTH_SHORT).show();
}
});
}
}
}
AndroidManifest.xml中
<service android:name=".MyService"
android:enabled="true"/>
- 1. WP7:如何显示敬酒?
- 2. 我怎么能显示3敬酒延迟1秒每个
- 3. Android的服务......让敬酒
- 4. 显示的Windows从WinForm应用程序10敬酒
- 5. 显示敬酒消息
- 6. 显示敬酒,如下图所示
- 7. 如何在特定时间设置闹钟以显示敬酒
- 8. 如何显示在从类敬酒消息不延长活动
- 9. 每天敬酒通知
- 10. 如何在开始AsyncTask后10秒内显示祝酒
- 11. 在自助服务中敬酒
- 12. 显示使用Android的敬酒
- 13. 敬酒不是同时显示
- 14. Android - 在视图下方显示敬酒?
- 15. 每10秒启动一次服务
- 16. 如何长期敬酒?
- 17. 如何使用使用GCM意图在通知服务以显示敬酒的Android
- 18. 如何显示Android敬酒时间较长
- 19. 错误从服务的Android调用敬酒
- 20. Android:如何每10秒通过服务发送http请求
- 21. 如何在MapView上每10秒显示一次当前位置?
- 22. 对话后敬酒
- 23. Android中的敬酒
- 24. c#mvc每隔10秒从webapi显示数据
- 25. 异步任务和敬酒 - Android
- 26. 如何在应用程序中设置每日敬酒
- 27. 如何在android中创建敬酒
- 28. 如何在ApplicationSyncAdapter Android中敬酒?
- 29. 如何设置敬酒文本重心
- 30. 如何阅读敬酒通知?
“我有这样的课吗?请编辑它。”不,我们不是免费的编辑服务。解决这个问题,并提出一个具体的问题,你卡住了,我们会帮助你尽我们所能:) – ElGavilan 2014-11-06 13:49:44
我的英语很差! – user3404171 2014-11-06 13:51:39
user3404171不是。我从来没有想过,你不会说它作为第一语言 – ElGavilan 2014-11-06 13:53:04