标签: android-handler

如果我在服务类上调用bindService后调用startService会发生什么?

我在Service MessengerService上调用bindService.它工作正常.之后,我调用startService.

代码与此链接远程信使服务示例部分 http://developer.android.com/reference/android/app/Service.html 完全相同,只是我在活动中添加了一个startService

这是客户端代码:Intent intnt = new Intent(context,MessengerService.class); intnt.putExtra("msg","从服务到处理程序11的活动的字符串");

    bindService(intnt, mConnection, Context.BIND_AUTO_CREATE);

    intnt.putExtra("msg", "String from activity to service to handler 22");     

    startService(intnt);
Run Code Online (Sandbox Code Playgroud)

在服务代码中:在onStartCommand中,无论我在startService中传递的意图中收到什么消息,我都将其发送回客户端处理程序.

我在行mClients.get(0).send(msg1)中获取索引超出绑定异常.mClients是附加到此服务的客户端数组,在绑定过程中存储.

代码与此链接远程信使服务示例部分 http://developer.android.com/reference/android/app/Service.html 完全相同,只是我在服务中添加onStartCommand

@Override
public int onStartCommand(Intent intent, int flags, int startId){

    String str = intent.getStringExtra("msg");
    Message msg1 = Message.obtain(null, MSG_STR_VALUE);
    Bundle data = new Bundle();
    data.putString("message", str);
    msg1.setData(data);

    System.out.println(str);
    try {
        s1.acquire();
    } catch (InterruptedException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    try {
        mClients.get(0).send(msg1);
    } catch (RemoteException e) {
        e.printStackTrace(); …
Run Code Online (Sandbox Code Playgroud)

android android-service android-handler

5
推荐指数
1
解决办法
989
查看次数

Android - Thread和AsyncTask之间的区别?

在我的应用程序中我有按钮,单击时将查询数据库并在屏幕上显示结果.查询操作通常需要1~3秒.这些按钮将经常被点击.

我已经在AsyncTask和Thread上实现了这个动作,但看起来差别很小.

但是从长远来看,尤其是在按钮被多次点击时,这在资源(CPU,内存)方面会更有益吗?

java android android-asynctask android-handler java-threads

5
推荐指数
1
解决办法
1939
查看次数

使用AlarmManager与Handler Android的不同情况

有人可以解释我使用AlarmManager vs Handler的不同情况.使用这两者作为彼此交替的任何缺点?

谢谢.

android alarmmanager android-handler

4
推荐指数
1
解决办法
3040
查看次数

如何在android中设置Delay for loop?

我试图显示带延迟的乘法表.我的代码工作正常,但我无法实现延迟.

这是我的代码:

tableButton1 = (Button) findViewById(R.id.Button1);
tableButton1.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {       
        new Thread(new Runnable() {
             public void run() {
                    str = tableButton1.getText().toString();  
                     a = Integer.parseInt(str);
                     StringBuilder sb = new StringBuilder();
                     for (int i = 1; i <= 10; i++){
                        sb.append(a + " x " + i + " = " + i * a+ "\n");

                     }
                     s = String.valueOf(sb);

                    Intent intent=new Intent(getApplicationContext(),TextViewActivity.class);
                    intent.putExtra("MA", s);
                    startActivity(intent);
             }
         });
         //Toast.makeText(getApplicationContext(), "Hi"  +ss, 222).show();              
    }       
}); 
Run Code Online (Sandbox Code Playgroud)

任何答案都很明显.

提前致谢

更新代码: …

multithreading android android-handler

4
推荐指数
1
解决办法
4315
查看次数

如何在Kotlin中使用Handler和handleMessage?

Java代码:

private final Handler mHandler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
       // code here
    }
};
Run Code Online (Sandbox Code Playgroud)

如何将这个Java代码转换为Kotlin?

我试过这个:

private val mHandler = object : Handler() {
    fun handleMessage(msg: Message) {
       // code here
    }
}
Run Code Online (Sandbox Code Playgroud)

但这似乎是不正确的,并给出了编译时错误 object

android kotlin android-handler

4
推荐指数
2
解决办法
4371
查看次数

在finish()之后多长时间将Runnables发布到Activity的Handler继续执行?

我有一个Handler链接到UI线程.正如所料,我可以post() Runnable使用它并让它们在UI线程上执行.我也有一个按钮,按下该按钮将调用时finish()Activity.

如何finish()影响UI处理程序的消息队列?它会处理队列中的所有消息但不再接受吗?它会直言不讳地拒绝运行并立即终止(我有理由相信这不是真的).是否会继续允许Runnables发布到它直到onDestroy()被调用?您应该假设活动堆栈只包含一个活动.

android android-activity android-handler

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

此Handler类应该是静态的,否则可能会发生泄漏(com.test.test3.ui.MainActivity.1)

我是android的新手,我尝试开发一个系统但是当我完成代码时,处理程序会显示此警告

下面显示我编辑后的代码,事件ontounch中的处理程序显示警告处理程序无法解析.我尝试将//忽略处理程序,我尝试运行应用程序并将其结果强制关闭.

public class MainActivity extends Activity {



protected static final int STOP = 100;
ImageView iv;
private ProgressBar pb;
LinearLayout ll;
private AnimationDrawable anim;
ScrollView sv;
private SQLiteDatabase db;
private boolean flagscanning = false;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ll = new LinearLayout(this);
    new HandlerClass(this);

            db = SQLiteDatabase.openDatabase(Environment.getExternalStorageDirectory()+"/antivirus.sqlite", null, SQLiteDatabase.OPEN_READONLY);  
            iv = (ImageView) this.findViewById(R.id.imageView1);
                    //???????
            pb = (ProgressBar) this.findViewById(R.id.progressBar1);
            ll = (LinearLayout) this.findViewById(R.id.ll);
                    //??ImageView?????????
            iv.setBackgroundResource(R.drawable.bg);
                    //sv???????????
            sv = (ScrollView) this.findViewById(R.id.scrollView1);
            anim = (AnimationDrawable) iv.getBackground();
}

private …
Run Code Online (Sandbox Code Playgroud)

android memory-leaks static-class android-handler

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

Handler.postDelayed(...)不会延迟Runnable

我自己实现了一个OnCompletionListener,如下所示:

public class SongEndCompletionListener  implements OnCompletionListener{

    String nextView;
    Context actualActivity;
    int stopTime;

    public SongEndCompletionListener(Context activity, String nextView, int time) {
        this.nextView = nextView;
        actualActivity = activity;
    }
    @Override
    public void onCompletion(MediaPlayer arg0) {


            Handler handler = new Handler(); 
            handler.postDelayed(new Runnable() { 
                 public void run() { 
                        try {
                     Intent stopplay;
                     stopplay = new Intent(actualActivity,Class.forName(nextView));
                     actualActivity.startActivity(stopplay);    
                 } catch (ClassNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    } 
                 } 
            }, stopTime); 


    }
}
Run Code Online (Sandbox Code Playgroud)

我想让它暂停一下stopTime秒,但它实际上做的是一旦audiofile结束它跳转到下一个视图.你能指点我走向错误的地方,或者我如何能够以不同的方式延迟切换到另一个活动?

每个提示都表示赞赏!

android runnable postdelayed android-mediaplayer android-handler

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

Handler.handleMessage在测试中未调用,但在应用程序中调用

我有一个在单独进程中运行的服务.该服务在onCreate()方法中生成一个新线程.该线程将消息发送回服务.

如果我手动启动应用程序一切正常 - Handler我的服务收到消息.但在我的测试handleMessage()方法中永远不会被调用.

如何修复测试以使handleMessage()方法有效?

谢谢!

服务:

public class MyService extends Service {

    private ServiceHandler mHandler;
    private final int MSG_HELLO_WORLD = 1;

    private final String TAG = getClass().getSimpleName();

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        return null;
    }


    @Override
    public void onCreate() {
        Log.d(TAG, "MyService.onCreate");
        super.onCreate();

        mHandler = new ServiceHandler();

        new Thread(new Runnable() {
            @Override
            public void run() {
                Log.d(TAG, "Thread: run()");

                while (true) { …
Run Code Online (Sandbox Code Playgroud)

java android android-service android-testing android-handler

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

new Handler() 和 new Handler(Looper.myLooper()) 之间的区别

我浏览了官方文档,但我似乎找不到new Handler()和之间是否有任何区别new Handler(Looper.myLooper())

new Handler()

默认构造函数将此处理程序与当前线程的 Looper 相关联。

Looper myLooper ()

返回与当前线程关联的Looper对象。如果调用线程未与 Looper 关联,则返回 null。

android android-handler

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