相关疑难解决方法(0)

getContext(),getApplicationContext(),getBaseContext()和"this"之间的区别

是什么区别getContext(),getApplicationContext(),getBaseContext(),和" this"?

虽然这是一个简单的问题,但我无法理解它们之间的基本区别.如果可能,请举出一些简单的例子.

android this android-context

535
推荐指数
5
解决办法
27万
查看次数

将服务绑定到Android中的活动

我正在尝试编写一个使用RTSP播放流式音频的简单媒体播放器.我有一个GUI活动和一个执行播放的服务.我的问题是如何在活动和服务之间进行最佳沟通(例如,根据玩家状态更新gui).

我知道我可以使用onBind()将服务绑定到活动,但如果我理解正确,如果活动被终止,这将停止服务.即使用户退出活动,我也想继续播放.有没有任何标准或首选的方法来处理这个问题?

service android

87
推荐指数
4
解决办法
12万
查看次数

如何从服务获取数据到活动

在我的应用程序中,我有一个活动和服务...该服务将广播从GPS数据收集的消息...活动应该接收广播消息并更新UI ...

我的代码

public class LocationPollerDemo extends Activity {
    private static final int PERIOD = 10000; // 30 minutes
    private PendingIntent pi = null;
    private AlarmManager mgr = null;
    private double lati;
    private double longi;
    private ServiceReceiver serviceReceiver;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mgr = (AlarmManager) getSystemService(ALARM_SERVICE);

        Intent i = new Intent(this, LocationPoller.class);

        i.putExtra(LocationPoller.EXTRA_INTENT, new Intent(this, ServiceReceiver.class));
        i.putExtra(LocationPoller.EXTRA_PROVIDER, LocationManager.GPS_PROVIDER);

        pi = PendingIntent.getBroadcast(this, 0, i, 0);
        mgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), PERIOD, pi);

        DebugLog.logTrace("On Create Demo");
        Toast.makeText(this, "Location polling every 30 …
Run Code Online (Sandbox Code Playgroud)

android

21
推荐指数
3
解决办法
4万
查看次数

怀疑bindService

我有一些关于Android绑定服务的文章.该指南:http://developer.android.com/guide/components/bound-services.html ,约bindService(),说:

The `bindService()` method returns immediately without a value
Run Code Online (Sandbox Code Playgroud)

但这似乎不正确,因为这里方法的签名是

public abstract boolean bindService (Intent service, ServiceConnection conn, int flags)
Run Code Online (Sandbox Code Playgroud)

其中返回的布尔值描述如下:

If you have successfully bound to the service, true is returned; false is returned if the connection is not made so you will not receive the service object.
Run Code Online (Sandbox Code Playgroud)

所以问题是:为什么文档说的方法returns immediately without a value呢?而且,这里绑定是这样完成的:

void doBindService() {
    bindService(new Intent(Binding.this, 
            LocalService.class), mConnection, Context.BIND_AUTO_CREATE);
    mIsBound = true;
}
Run Code Online (Sandbox Code Playgroud)

我不明白这个意义mIsBound = true …

android android-service android-activity android-service-binding

3
推荐指数
1
解决办法
1222
查看次数