d-m*_*man 139 android android-service android-activity
安卓:
public class LocationService extends Service {
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
startActivity(new Intent(this, activity.class));
}
}
Run Code Online (Sandbox Code Playgroud)
我推出了这项服务 Activity
在Activity
如果条件满足启动
startService(new Intent(WozzonActivity.this, LocationService.class));
Run Code Online (Sandbox Code Playgroud)
从我LocationService
上面提到的无法启动Activity
,我怎样才能获得当前Activity
在服务类中运行的上下文?
d-m*_*man 325
从Service类内部:
Intent dialogIntent = new Intent(this, MyActivity.class);
dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(dialogIntent);
Run Code Online (Sandbox Code Playgroud)
And*_*ndy 18
我遇到了同样的问题,并且想让你知道以上都不适用于我.对我有用的是:
Intent dialogIntent = new Intent(this, myActivity.class);
dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
this.startActivity(dialogIntent);
Run Code Online (Sandbox Code Playgroud)
在我的子类中,存储在一个单独的文件中,我不得不:
public static Service myService;
myService = this;
new SubService(myService);
Intent dialogIntent = new Intent(myService, myActivity.class);
dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
myService.startActivity(dialogIntent);
Run Code Online (Sandbox Code Playgroud)
所有其他答案都给了我一个nullpointerexception
.
Jos*_*raz 15
更新 Android 10 及更高版本
不再允许从服务(前台或后台)启动活动。
仍然有一些限制可以在文档中看到
https://developer.android.com/guide/components/activities/background-starts
另外值得一提的是:虽然上面的答案在我们的任务处于后台时工作得很好,但如果我们的任务(由服务+某些活动组成)在前台(即我们的一个活动可见),我唯一可以使它工作的方式对用户)是这样的:
Intent intent = new Intent(storedActivity, MyActivity.class);
intent.setAction(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
storedActivity.startActivity(intent);
Run Code Online (Sandbox Code Playgroud)
我不知道ACTION_VIEW或FLAG_ACTIVITY_NEW_TASK在这里是否有任何实际用途.成功的关键是
storedActivity.startActivity(intent);
Run Code Online (Sandbox Code Playgroud)
当然还有FLAG_ACTIVITY_REORDER_TO_FRONT用于不再次实例化活动.祝你好运!
Android 10 及更高版本的新方法是 SYSTEM_ALERT_WINDOW 权限。操作方法如下:在 AndroidManifyingst.xml 文件中声明此权限
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
Run Code Online (Sandbox Code Playgroud)
在单击按钮时的 onCreate 方法中,调用辅助方法来显示一个对话框,这样如果用户选择允许权限,则将打开覆盖权限的设备设置。请注意,单击按钮上的这个只是为了让您了解如何调用权限。您可以根据应用程序要求以您喜欢的方式调用它。
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
showMessageForFloatingPermission("To use this feature requires over lay permission");
}
});
}
Run Code Online (Sandbox Code Playgroud)
下面声明了辅助方法吗?
//Helper method to show a dialog window
private void showMessageForFloatingPermission(String message) {
new android.app.AlertDialog.Builder(MainActivity.this)
.setMessage(message)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
checkFloatingPermission();
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//User opted not to use this feature
//finish();
}
})
.create()
.show();
}
//Helper method for checking over lay floating permission
public void checkFloatingPermission() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (!Settings.canDrawOverlays(this)) {
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
Uri.parse("package:" + getPackageName()));
startActivityFloatingPermission.launch(intent);//this will open device settings for over lay permission window
}
}
}
//Initialize ActivityResultLauncher. Note here that no need custom request code
ActivityResultLauncher<Intent> startActivityFloatingPermission = registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(),
new ActivityResultCallback<ActivityResult>() {
@Override
public void onActivityResult(ActivityResult result) {
if (result.getResultCode() == Activity.RESULT_OK) {
//Permission granted
}else{
//If there is no permission allowed yet, still a dialog window will open unless a user opted not to use the feature.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (!Settings.canDrawOverlays(MainActivity.this)) {
// You don't have permission yet, show a dialog reasoning
showMessageForFloatingPermission("To use this feature requires over lay permission");
}
}
}
}
});
Run Code Online (Sandbox Code Playgroud)
一旦您相应地实现了上述代码,您就可以从服务类启动任何活动。您的活动将以编程方式启动。
Intent intent = new Intent(this, MyActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
Run Code Online (Sandbox Code Playgroud)
基本上,您可以这样做。效果很好。顺便说一句,您可以按照自己的意愿操作此代码。希望这会有所帮助!
归档时间: |
|
查看次数: |
202250 次 |
最近记录: |