我有一个正在运行和监听麦克风输入的Android服务.我希望它在满足某个标准时启动活动.为了创建一个Intent,我需要应用程序上下文.我怎么才能得到它?
Intent i = new Intent(ctx, SONR.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
ctx.startActivity(i);
Run Code Online (Sandbox Code Playgroud)
以上行不会启动我的活动.
这是我的构造函数
public SONRClient(Context c, AudioRecord ar, int buffsize, final AudioManager am) {
theAudioManager = am;
theaudiorecord = ar;
bufferSize = buffsize;
ctx = c;
CLIENT_ON = true;
}
Run Code Online (Sandbox Code Playgroud)
这是我的onCreate
@Override
public void onCreate() {
try {
// LogFile.MakeLog("\n\nSONRClient CREATED");
clientStopReceiver = new StopReceiver();
ctx.registerReceiver(clientStopReceiver,
new IntentFilter(SONR.DISCONNECT_ACTION));
myByteReceiver = new SONRByteReceiver();
theListener = new MicSerialListener(
theaudiorecord, bufferSize, myByteReceiver);
theApplication = getApplication();
} catch (Exception e) {
e.printStackTrace();
ErrorReporter.getInstance().handleException(e);
}
}
Run Code Online (Sandbox Code Playgroud)
myByteReceiver …
java android android-intent android-service android-activity
我有一项服务,开始一项新的活动,而屏幕关闭.我希望活动保持暂停模式,而不是进入停止模式.有没有办法实现这个目标?
我正在构建一个将显示通知的应用程序,并且在点击通知时,我想打开一个透明的对话框,如活动,其中包含与该通知相关的选项.
我定义了一个透明的活动,并在这样的通知中注册了它.
Intent notificationIntent = new Intent(context, EasyToDoReminder.class);
PendingIntent contentIntent = PendingIntent
.getActivity(context, 0, notificationIntent, 0);
Run Code Online (Sandbox Code Playgroud)
活动开始,但主要活动在其后面进行了统计.如何独立打开提醒活动?
我并不想展示对话.我想将活动看作一个对话框.我已经在使用你提到的主题了.我还有一项主要活动.当我尝试启动像活动一样的透明对话框时,主要活动就是开始,然后开始执行像活动这样的对话框.
任何人都可以建议以更好的方法做到这一点?
我有一个Service当一个函数给我true时它将启动一个新的Activity但它需要5秒...
我已经读过这个issue,我在StackOverflow这个例子中找到了"避免"这个bug.
Intent intent = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
try {
pendingIntent.send();
} catch (PendingIntent.CanceledException e) {
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
但遗憾的是它没有Activity更快启动,我不需要处于初级状态(如果可能的话更好),但是我不想等待+5秒来启动新功能Activity,你是否知道要避免这种情况?
我正在使用,PendingIntent因为我发现那个人说它应该解决这个问题:
在没有5秒延迟的情况下按下HOME按钮后从服务启动一个活动
如果我按下back button它会自动启动它,0延迟,但我正在按下它home button.
我尝试在服务中使用意图,但当我尝试这样做:
Intent intent_facebook = new Intent (this,MainUploadToYoutube.class);
intent_facebook.putExtra("vid", vid);
startActivity(intent_facebook);
Run Code Online (Sandbox Code Playgroud)
在logcat上遇到这个错误:
Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
Run Code Online (Sandbox Code Playgroud)
所以我从这里尝试了这个:
Intent intent_facebook = new Intent(getBaseContext(), MainUploadToYoutube.class);
intent_facebook.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplication().startActivity( intent_facebook);
Run Code Online (Sandbox Code Playgroud)
但这没什么,我没有在logcat中得到错误
怎么了?
我有Service实现IntentService,在OnHandleIntent中我想启动活动.
这是行不通的:
Intent dialogIntent = new Intent(this, typeof(Activity1));
dialogIntent.AddFlags(ActivityFlags.NewTask);
this.StartActivity(dialogIntent);
Run Code Online (Sandbox Code Playgroud)
我还能尝试什么?
upd:AddFlags(ActivityFlags.NewTask); 它没有帮助
这是我的样子
A1 =启动画面活动
A2 =主要活动
A3 =额外活动
S1 = GPS服务
我从A1开始,它创建了启动A2的意图,然后启动A1。在A2内创建并绑定S1(在S1内发出通知)
CharSequence text = getText(R.string.local_service_started);
Notification notification = new Notification(R.drawable.notify_icon, text, System.currentTimeMillis());
Intent i = new Intent();
i.setClassName("xxx.yyy.zzz.kkk", "xxx.yyy.zzz.kkk.A2");
i.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(this, getText(R.string.local_service_label), text, contentIntent);
mNM.notify(NOTIFICATION, notification);
Run Code Online (Sandbox Code Playgroud)
现在,我在通知栏中显示了我的图标如果我在A2活动中按HOME按钮并打开另一个应用程序,然后按我的通知图标,则所有按钮均正常运行,并且我将我的A2活动(A2是最活跃的活动)回退了,但是如果在A2内启动A3并返回HOME并按通知我有问题,则A2被创建为新实例(A2现在不是最重要的)!可以像长按HOME一样使效果集中在应用程序中的最后一个打开的活动上吗?我不想打开特定的活动,但是在没有新的活动实例的情况下将我暂停的活动放在前面。
我有一个andoid-app,当用户登录时它会连接到WebSocket服务器。如果用户空闲了一段时间或出于某种原因他的会话到期/终止,则该应用程序会通过WebSocket连接得到通知,我想将他带回登录活动(如果他未登录,则应用程序中没有任何功能)。
我研究了广播的Intent和runOnUIThread(),但似乎无法使其正常工作。我的想法是简单地注册正在运行的活动(例如,让所有活动都实现一个接口)并进行调用,但这意味着所有活动都必须实现该接口。它们大多数已经继承自BaseActivity类,但并非全部。
我意识到它的坏习惯是突然中断用户流,但是除非用户具有有效的会话,否则所有功能都无法正常工作。
我使用android-websocket(https://github.com/koush/android-websockets)库作为客户端,如果有什么不同的话。
在此先感谢托马斯
编辑:有时我也想以Toast或Dialog的形式通知用户服务器上发生了某个事件。
我想轻松地将一个字符串传递给一个Activity.需要回调之类的东西,因为当必须传递字符串时,Activity必须做一些事情.
public class MyHostApduService extends HostApduService {
@Override
public byte[] processCommandApdu(byte[] apdu, Bundle extras) {
if (selectAidApdu(apdu)) {
Log.i("HCEDEMO", "Application selected");
return getWelcomeMessage();
}
else {
if (exchangeDataApdu(apdu)) {
Log.i("HCEDEMO", "Data exchanged");
// int _len_ = apdu[4];
String _data_ = new String(apdu).substring(5, apdu.length - 1);
// TODO: Send _data_ to an activity...
return new byte[] { (byte)0x90, (byte)0x00 };
}
else {
Log.i("HCEDEMO", "Received: " + new String(apdu));
return getNextMessage();
}
}
}
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一个即使我的应用程序关闭也能运行的服务.但是,我需要在此服务中使用我的应用上下文.当应用程序运行时,该服务也可以正常工作,但是当我关闭应用程序(调用onDestroy())时,始终返回.getContext()null
服务
public class SubscribeService extends Service {
private Context context;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
context = this; //Returns null when service is running on background
context = MyApp.getContext(); //Also null
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
//do stuff using context
}
Run Code Online (Sandbox Code Playgroud)
MyApp的
public class MyApp extends Application {
private static Context context;
public static Context …Run Code Online (Sandbox Code Playgroud) 我创建了一个“始终在顶部按钮”的叠加层,这是一项服务HUD,我无法screen从那里开始活动,它给出了错误:"Unfortunately App has stopped"。一开始,我只知道是否有TouchEvent一个 toast,那个 toast 被创建了,但是它被创建了好几次,所以我不知道它是否给出了那个错误,因为这段代码,在TouchEventbody 上,也重复了几次。这是我的代码:
public class HUD extends Service implements OnClickListener, OnTouchListener, OnLongClickListener {
Button mButton;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
//mView = new HUDView(this);
mButton = new Button(this);
mButton.setId(1);
mButton.setText("Button");
mButton.setClickable(true);
mButton.setOnTouchListener(this);
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.OPAQUE);
params.gravity = Gravity.LEFT | Gravity.TOP;
params.setTitle("Load Average");
WindowManager wm = (WindowManager) …Run Code Online (Sandbox Code Playgroud)