求助程序无响应,求帮忙分析一下。为什么出现这种情况,如何解决。

K_night 发布于 2013/03/01 11:42
阅读 450
收藏 0

本人新入行软件领域,写了一个安卓客户端程序,activity有个按钮,单击事件之后激活service后台运行,service采用HTTP协议,每30秒访问一次servlet,服务器tomcat,程序运行的时候,在模拟器上运行很正常,在真机器上运行,30分钟左右正常,30分钟以后,程序无响应!为了省电程序用了WakeLock锁,在访问服务器结束之后,释放锁,代码如下,新人求助。帮忙分析一下。为什么出现这种情况,如何解决。

HTTP_Server_Time_Activity.java

 

package com.example.http_server_time;

import com.example.http_server_time.R;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;

public class HTTP_Server_Time_Activity extends Activity {
 private Button login, cancel;
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_http__server__time);
  login = (Button) this.findViewById(R.id.yes);
  cancel = (Button) this.findViewById(R.id.no);
  login.setOnClickListener(onClickListenr);
  cancel.setOnClickListener(onClickListenr);
 }
 private View.OnClickListener onClickListenr = new View.OnClickListener() {
  @Override
  public void onClick(View v) {
   // TODO Auto-generated method stub
   if (v.getId() == R.id.yes) {
    System.out.println("选择了启动服务");
    Intent intent = new Intent();
    intent.setClass(HTTP_Server_Time_Activity.this, FirstService.class);
    startService(intent);
   }else if (v.getId() == R.id.no) {
    System.out.println("选择了停止服务");
    Intent intent = new Intent();
    intent.setClass(HTTP_Server_Time_Activity.this, FirstService.class);
    stopService(intent);
   }
  }
 };

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  // Inflate the menu; this adds items to the action bar if it is present.
  getMenuInflater().inflate(R.menu.activity_http__server__time, menu);
  return true;
 }

}

 

FirstService.java

 

package com.example.http_server_time;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import com.example.http_server_time.R;

import android.app.AlarmManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.os.PowerManager;

public class FirstService extends Service {
 PowerManager pm;
 PowerManager.WakeLock wakeLock;
 private PendingIntent pendingIntent;
 private AlarmManager alarmManager;
 // 通知管理器
 private NotificationManager nm;
 // 通知显示内容
 private PendingIntent pd;
 private Notification baseNF;
 int i = 1;
 int j = 1;

 @Override
 public IBinder onBind(Intent intent) {
  // TODO Auto-generated method stub
  System.out.println("Service onBind");
  return null;
 }

 // 当创建一个Servcie对象之后,会首先调用这个函数
 @Override
 public void onCreate() {
  acquireWakeLock();
  // TODO Auto-generated method stub
  alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
  Intent myIntent = new Intent(FirstService.this, FirstService.class);
  pendingIntent = PendingIntent.getService(FirstService.this, 0,
    myIntent, 0);
  alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
    System.currentTimeMillis(), 30 * 1000, pendingIntent);
  super.onCreate();
  System.out.println("Service onCreate");
 }

 @Override
 public int onStartCommand(Intent intent, int flags, int startId) {
  fangwen();
  System.out.println("Service onStartCommand");
  releaseWakeLock();
  // handler.postDelayed(runnable, 1000);// 打开定时器,执行操作
  return START_NOT_STICKY;
  
 }

 // 访问服务器

 public void fangwen() {
  // TODO Auto-generated method stub

  System.out.println("Service onStartCommand");
  // 要访问的HttpServlet
  String urlStr = "http://192.168.8.92:8080/DinnerClick/LoginServlet?";
  // 要传递的数据
  try {
   URL url = new URL(urlStr);

   // 获得连接

   HttpURLConnection conn = (HttpURLConnection) url.openConnection();

   if (true) {

    // 获得输入流

    InputStream in = conn.getInputStream();

    // 创建一个缓冲字节数

    byte[] buffer = new byte[20];

    // 在输入流中读取数据并存放到缓冲字节数组中

    in.read(buffer);

    // 将字节转换成字符串

    String msg = new String(buffer);
    System.out.println(msg);
    in.close();// 关闭数据流
    tuisong(msg);

   }

  } catch (Exception e) {
   System.out.println("异常");
  }
 }

 @Override
 public void onDestroy() {
  AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
  alarmManager.cancel(pendingIntent);
  // TODO Auto-generated method stubo
  System.out.println("Service onDestory");
  // handler.removeCallbacks(runnable);// 关闭定时器处理
  super.onDestroy();
 }

 public void tuisong(String ly_msg) {
  nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
  Intent intent = new Intent(this, FirstService.class);
  pd = PendingIntent.getActivity(FirstService.this, 0, intent, 0);
  baseNF = new Notification();
  // 此处采用默认声音
  baseNF.defaults |= Notification.DEFAULT_SOUND;
  baseNF.defaults |= Notification.DEFAULT_VIBRATE;
  baseNF.defaults |= Notification.DEFAULT_LIGHTS;
  // 让声音、振动无限循环,直到用户响应
  // baseNF.flags |= Notification.FLAG_INSISTENT;
  baseNF.flags |= Notification.FLAG_ONLY_ALERT_ONCE;
  // 通知被点击后,自动消失
  baseNF.flags |= Notification.FLAG_AUTO_CANCEL;
  // 点击'Clear'时,不清楚该通知(QQ的通知无法清除,就是用的这个)
  baseNF.flags |= Notification.FLAG_NO_CLEAR;
  // 第二个参数 :下拉状态栏时显示的消息标题 expanded message title
  // 第三个参数:下拉状态栏时显示的消息内容 expanded message text
  // 第四个参数:点击该通知时执行页面跳转

  String str1 = "ContentOne---" + i + " " + ly_msg;
  String str2 = "ContentTwo---" + j + " " + ly_msg;
  if (select()) {
   baseNF.icon = R.drawable.aa;
   baseNF.tickerText = "Numbber 1!";
   baseNF.setLatestEventInfo(FirstService.this, "Title01", str1, pd);
   nm.notify(0, baseNF);
   i++;
  } else {
   baseNF.icon = R.drawable.bb;
   baseNF.tickerText = "Number 2!";
   baseNF.setLatestEventInfo(FirstService.this, "Title02", str2, pd);
   nm.notify(1, baseNF);
   j++;
  }
 }

 public boolean select() {
  System.out.println("1");
  int a = (int) (Math.random() * 2);
  if (a == 1) {
   return true;
  } else {
   return false;
  }
 }

 private void acquireWakeLock() {
  if (wakeLock == null) {
   System.out
     .println("-------------------------------------------------------");
   pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
   wakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, this
     .getClass().getCanonicalName());
   wakeLock.acquire();
  }
 }

 private void releaseWakeLock() {
  // is held 运行 && wakeLock.isHeld()
  if (wakeLock != null) {
   wakeLock.release();
   wakeLock = null;
  }
 }
}

activity_http__server__time.xml

<?xml version = "1.0" encoding = "UTF-8" ?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <TextView
        android:id="@+id/username"
        android:layout_width="fill_parent"
        android:layout_height="50dp"
        android:text="@string/xianshi"
        />
 <Button
        android:id="@+id/yes"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/yes"
        />
    <Button
        android:id="@+id/no"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/no"
        />
</LinearLayout>

AndroidManifest.xml

 

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.http_server_time"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="10" />
 <uses-permission android:name="android.permission.INTERNET"></uses-permission>
 <uses-permission android:name="android.permission.VIBRATE"></uses-permission>
 <uses-permission android:name="android.permission.WAKE_LOCK"/>
    <application
        android:allowBackup="true"
        android:icon="@drawable/c"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.http_server_time.HTTP_Server_Time_Activity"
            android:label="@string/app_name" >
            <intent-filter>
                 <action android:name="android.intent.action.MAIN" />
     <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service android:name="com.example.http_server_time.FirstService"></service>
    </application>

</manifest>

 

 

 

 


HTTP_Server_Time_ActivityHTTP_Server_Time_ActivityHTTP_Server_Time_ActivityHTTP_Server_Time_ActivityHTTP_Server_Time_ActivityHTTP_Server_Time_ActivityHTTP_Server_Time_ActivityHTTP_Server_Time_ActivityHTTP_Server_Time_ActivityHTTP_Server_Time_ActivityHTTP_Server_Time_Activity

 

加载中
返回顶部
顶部