我有一个Android应用程序,我用MyApp.java启动我的应用程序,在那里我声明了所有内容,这是我的应用程序的起点。
在onCreate()
中,我有意图启动服务。
我还创建了一个服务实例,并尝试访问倒数计时器onFinish()
内的服务对象,作为 -
MYService myService = new MYService();
myService.getDialog();
在MYService中,我将此方法设为 -
public void showMsg(){
AlertDialog alertDialog = new AlertDialog.Builder(getApplication())
.setTitle("Success!")
.setMessage("Countdown")
.setPositiveButton("Success", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
}).create();
alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
alertDialog.show();
}
我的目标是我所在的应用程序中的任何一个部分,无论哪个活动或片段,我必须能够在倒数计时器5秒后获得此警报。 我如何实现这一目标?
答案 0 :(得分:0)
您可以使用BoundService
:
答案 1 :(得分:0)
在你的清单中
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name=".ShowAlertService"
android:label="@string/app_name"/>
在您的MainActivity.JAVA
中 import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent(getApplicationContext(),ShowAlertService.class);
startService(intent);
}
@Override
public void onResume() {
super.onResume();
// This registers mMessageReceiver to receive messages.
LocalBroadcastManager.getInstance(this)
.registerReceiver(mMessageReceiver,
new IntentFilter("broadcastMsg"));
}
// Handling the received Intents for the "my-integer" event
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getBooleanExtra("showalert",false))
{
showMsg();
}
}
};
@Override
protected void onPause() {
// Unregister since the activity is not visible
LocalBroadcastManager.getInstance(this)
.unregisterReceiver(mMessageReceiver);
super.onPause();
}
public void showMsg(){
AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this)
.setTitle("Success!")
.setMessage("Countdown")
.setPositiveButton("Success", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
}).create();
alertDialog.show();
}
}
最后你的服务类就像
import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
import java.util.Timer;
import java.util.TimerTask;
public class ShowAlertService extends Service {
Timer timer;
TimerTask timerTask;
String TAG = "Timers";
int Your_X_SECS = 5;
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.e(TAG, "onStartCommand");
super.onStartCommand(intent, flags, startId);
startTimer();
return START_STICKY;
}
@Override
public void onCreate() {
Log.e(TAG, "onCreate");
}
@Override
public void onDestroy() {
Log.e(TAG, "onDestroy");
stoptimertask();
super.onDestroy();
}
//we are going to use a handler to be able to run in our TimerTask
final Handler handler = new Handler();
public void startTimer() {
//set a new Timer
timer = new Timer();
//initialize the TimerTask's job
initializeTimerTask();
//schedule the timer, after the first 5000ms the TimerTask will run every 10000ms
timer.schedule(timerTask, 5000, Your_X_SECS * 1000); //
//timer.schedule(timerTask, 5000,1000); //
}
public void stoptimertask() {
//stop the timer, if it's not already null
if (timer != null) {
timer.cancel();
timer = null;
}
}
public void initializeTimerTask() {
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() {
sendMessage();
}
});
}
};
}
//Send message to activity
private void sendMessage() {
Intent intent = new Intent("broadcastMsg");
intent.putExtra("showalert",true);
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}
}
将广播消息“showalert”发送到活动,并在收到广播消息后立即显示警报。