0

我目前正在使用AlarmManager、Broadcast接收器和Intent Service通过后台而不是Ui主线程来实现向服务器摩擦的上传服务,但问题是在执行时,上传消息没有响应。至少,在 logcat 中,我可以看到任何报告上传进度的消息,即使我已经正确输入和测试。

但是上传服务完成后,它显示 java.lang.RuntimeException: Handler (android.os.Handler) 在死线程上向 Handler 发送消息

你能告诉我还缺少什么吗?

以下是我的代码

内部服务

public class TaskService extends IntentService {

    public TaskService() {
        super("TaskService");
        // TODO Auto-generated constructor stub
    }

    @Override
    protected void onHandleIntent(Intent arg0) {

        // Do some task
        Log.i("TaskService","Service running");
        boolean ss = uploadRecording("TEST_RECORD" , "test.mp4" , "http://210.177.246.83/uploadFile");
        Log.d("Is file uploaded" , String.valueOf(ss));
    }

    public boolean uploadRecording(String directoryname , String filename , String destination) {
        // TODO Auto-generated method stub

        boolean result = false;

        String destinationPath = destination;       

        File tes = new File(Environment.getExternalStorageDirectory() + File.separator + directoryname);
        File frecord = new File(tes.getAbsolutePath() + File.separator + filename);
        if(tes.exists()){
            if(frecord.exists()){

                List< NameValuePair> httpContents = new ArrayList< NameValuePair>();
                httpContents.add(new BasicNameValuePair("file",frecord.getAbsolutePath())); 

                HttpClient client=new DefaultHttpClient();
                HttpPost post=new HttpPost(destinationPath);
                try{
                    //setup multipart entity
                    MultipartEntity entity=new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);

                    for(int i=0;i< httpContents.size();i++){
                        //identify param type by Key
                        if(httpContents.get(i).getName().equals("file")){
                            File f=new File(httpContents.get(i).getValue());
                            FileBody fileBody=new FileBody(f);
                            entity.addPart("file"+i,fileBody);
                        }
                    }
                    post.setEntity(entity);
                    //create response handler
                    //execute and get response
                    HttpResponse uploadReponse = client.execute(post);
                    Log.d("debug" , "Response : " + uploadReponse);
                    if(uploadReponse.getStatusLine().getStatusCode() == 200){
                        result = true;
                        Toast.makeText(getApplicationContext(), "Upload Success", Toast.LENGTH_SHORT).show();
                    }
                }catch(Exception e){
                    e.printStackTrace();
                }     
            }
        }

        return result;       
    }   
}

广播接收器

public static String ACTION_ALARM = "com.alarammanager.alaram";

    @Override
    public void onReceive(Context context, Intent intent) {

        Log.i("Alarm Receiver", "Entered");
        Toast.makeText(context, "Entered", Toast.LENGTH_SHORT).show();

        Bundle bundle = intent.getExtras();
        String action = bundle.getString(ACTION_ALARM);
        if (action.equals(ACTION_ALARM)) {
            Log.i("Alarm Receiver", "If loop");
            Toast.makeText(context, "If loop", Toast.LENGTH_SHORT).show();
            Intent inService = new Intent(context, TaskService.class);
            inService.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startService(inService);
        }
        else
        {
            Log.i("Alarm Receiver", "Else loop");
            Toast.makeText(context, "Else loop", Toast.LENGTH_SHORT).show();
        }
    }

主要活动

public class AlaramScheduleActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

    }

    public void btnStartSchedule(View v) {

        try {
            AlarmManager alarms = (AlarmManager) this
                    .getSystemService(Context.ALARM_SERVICE);

            Intent intent = new Intent(getApplicationContext(),
                    AlaramReceiver.class);
            intent.putExtra(AlaramReceiver.ACTION_ALARM,
                    AlaramReceiver.ACTION_ALARM);

            final PendingIntent pIntent = PendingIntent.getBroadcast(this,
                    1234567, intent, PendingIntent.FLAG_UPDATE_CURRENT);

            alarms.setRepeating(AlarmManager.RTC_WAKEUP,
                    System.currentTimeMillis(), 1000 * 10, pIntent);

            toast("Started...");

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    public void btnCancelSchedules(View v) {

        Intent intent = new Intent(getApplicationContext(),
                AlaramReceiver.class);
        intent.putExtra(AlaramReceiver.ACTION_ALARM,
                AlaramReceiver.ACTION_ALARM);

        final PendingIntent pIntent = PendingIntent.getBroadcast(this, 1234567,
                intent, PendingIntent.FLAG_UPDATE_CURRENT);

        AlarmManager alarms = (AlarmManager) this
                .getSystemService(Context.ALARM_SERVICE);

        alarms.cancel(pIntent);
        toast("Canceled...");
    }

    public void toast(String message) {
        Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT)
                .show();
    }

安卓清单

已获得 INTERNET Permisson

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

   <service android:name=".TaskService" >
</service>

<receiver
    android:name="AlaramReceiver"
    android:process=":remote" >
</receiver>

4

1 回答 1

1

您的问题出在 IntentService 显示的 Toast 消息中。修复很简单:在您的服务的 onCreate 中创建 Handler 并发布 Runnable 以从中显示 toast。

public class TaskService extends IntentService {
...
    private Handler mHandler;

    protected void onCreate() {
        mHandler = new Handler();
    }

    ...

    public boolean uploadRecording(... {
    ...
        mHandler.post(new Runnable() {
            public void run() {
                Toast.makeText(getApplicationContext(), "Upload Success", Toast.LENGTH_SHORT).show();
            }
        });
    ...
    }

}

Toast 内部使用 Handler 与 INotificationManager 进行通信。一旦创建了 toast,它就会创建 Handler 的实例,该实例使用它创建的线程中的 looper。在您的情况下,它是 IntentService 的 HandlerThread。一旦服务完成所有工作,它就会中断。这就是为什么它已经死了。

于 2014-09-05T16:23:18.883 回答