我在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) 在我的应用程序中我有按钮,单击时将查询数据库并在屏幕上显示结果.查询操作通常需要1~3秒.这些按钮将经常被点击.
我已经在AsyncTask和Thread上实现了这个动作,但看起来差别很小.
但是从长远来看,尤其是在按钮被多次点击时,这在资源(CPU,内存)方面会更有益吗?
有人可以解释我使用AlarmManager vs Handler的不同情况.使用这两者作为彼此交替的任何缺点?
谢谢.
我试图显示带延迟的乘法表.我的代码工作正常,但我无法实现延迟.
这是我的代码:
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)
任何答案都很明显.
提前致谢
更新代码: …
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
我有一个Handler链接到UI线程.正如所料,我可以post() Runnable使用它并让它们在UI线程上执行.我也有一个按钮,按下该按钮将调用时finish()的Activity.
如何finish()影响UI处理程序的消息队列?它会处理队列中的所有消息但不再接受吗?它会直言不讳地拒绝运行并立即终止(我有理由相信这不是真的).是否会继续允许Runnables发布到它直到onDestroy()被调用?您应该假设活动堆栈只包含一个活动.
我是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) 我自己实现了一个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
我有一个在单独进程中运行的服务.该服务在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
我浏览了官方文档,但我似乎找不到new Handler()和之间是否有任何区别new Handler(Looper.myLooper())
new Handler()
默认构造函数将此处理程序与当前线程的 Looper 相关联。
Looper myLooper ()
返回与当前线程关联的Looper对象。如果调用线程未与 Looper 关联,则返回 null。
android ×10
android-handler ×10
java ×2
alarmmanager ×1
java-threads ×1
kotlin ×1
memory-leaks ×1
postdelayed ×1
runnable ×1
static-class ×1