package com.hiwi;
import java.io.IOException;
import java.net.URL;
import java.util.Calendar;
import java.util.Locale;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.mail.Address;
import javax.mail.BodyPart;
import javax.mail.Flags;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.NoSuchProviderException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.Flags.Flag;
import javax.mail.search.FlagTerm;
import org.apache.cordova.DroidGap;
import org.opencv.javacv.facerecognition.R;
import com.hiwi.HeartbeatsActivity.ReadMailSample;
import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationCompat.Builder;
import android.util.Log;
import android.webkit.WebView;
public class HeartbeatsActivity extends DroidGap {
public static String macAddr;
BluetoothAdapter adapter = null;
JSInterface js;
ReadMailSample rd;
private static final int MY_DATA_CHECK_CODE = 1234;
public static Context c,b;
public TextToSpeech tts;
public static Intent serviceIntent;
private static int myNotificationId;
/** Called when the activity is first created. */
CustomNativeAccess cna;
//face recognition activity
@Override
public void onCreate(Bundle savedInstanceState) {
c = this.getApplicationContext();
WifiManager wifiMan = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInf = wifiMan.getConnectionInfo();
macAddr = wifiInf.getMacAddress();
super.onCreate(savedInstanceState);
super.init();
super.setIntegerProperty("splashscreen", R.drawable.splashscreen);
js=new JSInterface(this,appView);
appView.addJavascriptInterface(js, "JSI");
cna = new CustomNativeAccess(this, appView);
appView.addJavascriptInterface(cna, "CustomNativeAccess");
// rd= new ReadMailSample();
//appView.addJavascriptInterface(rd, "RD");
ReadMailSample rd=new ReadMailSample(getApplicationContext());
System.out.println("hello");
rd.execute();
}
public static class ReadMailSample extends AsyncTask<String,String,Void> implements TextToSpeech.OnInitListener{
Message message;
private TextToSpeech tts;
static String command, phoneNumber, type, priority, name, time_stamp, imei, opt1, opt2, opt3, fromSubString;
Properties properties = null;
private Session session = null;
private Store store = null;
private Folder inbox = null;
String userName="avarote1994@gmail.com" ; // PROVIDE RECEPIENT EMAIL ID
String password="amul11111994" ; // PROVIDE RECEPIENT PASSWORD
static SQLiteDatabase db;
boolean flag=false;
Context acn;
//private Bundle savedInstanceState;
protected Void doInBackground(String...params){ // SEPARATE THREAD TO RUN IN THE BACKGROUND
try{
readMails();
}
catch(Exception e){
Logger logger = Logger.getAnonymousLogger();
logger.log(Level.INFO, "an exception was thrown", e);
}
return null;
}
ReadMailSample(SQLiteDatabase db){
this.db = db;
}
ReadMailSample(){
}
ReadMailSample(Context cn){
acn=cn;
}
@Override
protected void onPostExecute(Void unused) {
WebView web= new WebView(c);
web.loadUrl("file:///android_asset/www/login.html");
}
@Override
protected void onPreExecute() {
super.onPreExecute();
tts = new TextToSpeech(c,ReadMailSample.this);
}
@Override
protected void onProgressUpdate(String... values) {
try {
System.out.println("---------------------adasd-----------" + time_stamp);
showNotification();
WebView web= new WebView(c);
web.loadUrl("file:///android_asset/www/login.html");
//speakOut();
}
catch(Exception e){
e.printStackTrace();
}
}
@Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
tts.speak(command, TextToSpeech.QUEUE_FLUSH, null);
int result = tts.setLanguage(Locale.US);
if (result == TextToSpeech.LANG_MISSING_DATA
|| result == TextToSpeech.LANG_NOT_SUPPORTED) {
Log.e("TTS", "This Language is not supported");
} else {
speakOut();
}
} else {
Log.e("TTS", "Initilization Failed!");
}
}
public void speakOut() {
tts.speak("hello hi", TextToSpeech.QUEUE_FLUSH, null);
}
public void showNotification() {
PendingIntent notificationIntent = preparePendingIntent();
Notification notification = createBasicNotification(notificationIntent);
displayNotification(notification);
}
@SuppressLint("InlinedApi")
private PendingIntent preparePendingIntent() {
Intent intent=new Intent(c,MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(
c,
0,
intent,
PendingIntent.FLAG_UPDATE_CURRENT);
return pendingIntent;
}
private Notification createBasicNotification(PendingIntent notificationIntent) {
NotificationCompat.Builder builder = new Builder(c);
long[] vibrationPattern = {0, 200, 800, 200, 600, 200, 400, 200, 200, 200, 100, 200, 50, 200, 50, 200, 50, 200, 50, 200};
Notification notification = builder
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Medication Reminder")
.setContentText(command)
.setAutoCancel(true)
.setContentIntent(notificationIntent)
.setWhen(Calendar.getInstance().getTimeInMillis() + 1000*60+60)
.setVibrate(vibrationPattern)
.build();
return notification;
}
private void displayNotification(Notification notification) {
NotificationManager notificationManager = (NotificationManager)c.getSystemService(Context.NOTIFICATION_SERVICE);
myNotificationId=(int) System.currentTimeMillis();
notificationManager.notify(myNotificationId , notification);
}
public void readMails() throws IOException{
System.out.println("READMAIL hi");
properties = new Properties();
// SETTING UP AN IMAP SERVER TO ACCESS THE RECEPIENT'S EMAIL
properties.setProperty("mail.host", "imap.gmail.com");
properties.setProperty("mail.port", "995");
properties.setProperty("mail.transport.protocol", "imaps");
while(true){// CONTINUOUSLY MONITOR INCOMING MAIL'S
//String cq = "select * from Login4";
//Cursor c = db.rawQuery(cq, null);
// c.moveToFirst();
//final String userName = c.getString(0);
//final String password = c.getString(1);
//String cloud = "avarote1994@gmail.com";
// AUTHENTICATE AND GET AN INSTANCE OF THE SESSION FROM THE SERVER
session = Session.getInstance(properties,new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(userName, password);
}
});
try {
store = session.getStore("imaps");
store.connect();
inbox = store.getFolder("INBOX"); // ACCESS THE INBOX OF THE RECEPIENT'S EMAIL ID
inbox.open(Folder.READ_WRITE); // OPEN THE INBOX IN READ-WRITE MODE
Message messages[] = inbox.search(new FlagTerm(new Flags(Flag.SEEN), false)); //SEARCH INBOX FOR ANY UNREAD MAILS
System.out.println("Number of mails = " + messages.length);
for (int i = 0; i < messages.length; i++) { // PROCESS ALL THE UNREAD MAILS
message = messages[i];
Address[] from = message.getFrom();
String from1 = from[0].toString();
System.out.println(from1);
if(from1.contains("<")){
int start = from1.indexOf("<");
int end = from1.indexOf(">");
fromSubString = from1.substring(start+1,end); // RETRIEVE THE SENDER'S EMAIL ID
} else{
fromSubString = from1;
}
System.out.println(fromSubString);
//if(fromSubString.equals(cloud)){ // CHECK WHETHER THE MAIL IS FROM THE CLOUD
String[] subject = message.getSubject().split(","); // SPLIT THE SUBJECT
System.out.println("hi");
type = subject[0]; // STORE THE DETAILS IN RESPECTIVE VARIABLES
phoneNumber =subject[1];
name = subject[2];
System.out.println(type);
System.out.println(phoneNumber);
System.out.println(name);
//String body=message.getContentType().toString();
// System.out.print(body);
processMessageBody(message);
//System.out.println("--------------------------------");
// }
}
inbox.close(true);
store.close();
}
catch (NoSuchProviderException e) {
e.printStackTrace();
}
catch (MessagingException e) {
e.printStackTrace();
}
}
}
public void processMessageBody(Message message) {
try {
Object content = message.getContent();
String msg=content.toString();
System.out.println(msg);
if (content instanceof Multipart) { // IF MAIL HAS MULTIPART MESSAGE
Multipart multiPart = (Multipart) content;
procesMultiPart(multiPart);
}
else{
System.out.println("Content = "+content);
processSinglepart(content.toString());
}
}
catch (IOException e) {
e.printStackTrace();
}
catch (MessagingException e) {
e.printStackTrace();
}
}
public void processSinglepart(String content){
String[] body = content.split(","); // SPLIT THE CONTENTS OF THE BODY
System.out.println('1');
time_stamp = body[0]; // STORE THE DETAILS IN RESPECTIVE VARIABLES
command = body[3];
System.out.println(time_stamp);
//tts.speak(time_stamp, TextToSpeech.QUEUE_FLUSH, null);
publishProgress(command);
}
public void procesMultiPart(Multipart content) {
System.out.println("amulya");
try {
BodyPart bodyPart = content.getBodyPart(0); // RETRIEVE THE CONTENTS FROM THE BODY
Object o;
o = bodyPart.getContent();
if (o instanceof String) {
System.out.println("Content Multipart= "+o.toString());
processSinglepart(o.toString());
}
}
catch (IOException e) {
e.printStackTrace();
}
catch (MessagingException e) {
e.printStackTrace();
}
}
}
}
我正在尝试将接收邮件asyc任务集成到droid gap class。我正在致命的例外。我怎样才能整合这两个单独的应用程序?这涉及到java脚本。我希望异步运行后台而不影响应用程序的其他功能。
Stack Trace如下:
02-08 22:54:31.139:I / System.out(15241):1221235487821 02-08 22:54:31.139:I / System.out(15241):--------------------- adasd ---------- -1221235487821 02-08 22:54:34.819:I / System.out(15241):邮件数量= 0 02-08 22:54:37.599:D / DroidGap(15241):暂停了应用程序! 02-08 22:54:37.599:D / CordovaWebView(15241):处理暂停 02-08 22:54:37.599:D / AndroidRuntime(15241):关闭VM 02-08 22:54:37.599:W / dalvikvm(15241):threadid = 1:线程退出时未捕获异常(组= 0x433111a0) 02-08 22:54:37.609:E / AndroidRuntime(15241):致命异常:主要 02-08 22:54:37.609:E / AndroidRuntime(15241):进程:org.opencv.javacv.facerecognition,PID:15241 02-08 22:54:37.609:E / AndroidRuntime(15241):java.lang.RuntimeException:无法暂停活动{org.opencv.javacv.facerecognition / com.hiwi.HeartbeatsActivity}:java.lang.NullPointerException 02-08 22:54:37.609:E / AndroidRuntime(15241):在android.app.ActivityThread.performPauseActivity(ActivityThread.java:3397) 02-08 22:54:37.609:E / AndroidRuntime(15241):在android.app.ActivityThread.performPauseActivity(ActivityThread.java:3352) 02-08 22:54:37.609:E / AndroidRuntime(15241):在android.app.ActivityThread.handlePauseActivity(ActivityThread.java:3330) 02-08 22:54:37.609:E / AndroidRuntime(15241):在android.app.ActivityThread.access $ 1100(ActivityThread.java:172) 02-08 22:54:37.609:E / AndroidRuntime(15241):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1319) 02-08 22:54:37.609:E / AndroidRuntime(15241):在android.os.Handler.dispatchMessage(Handler.java:102) 02-08 22:54:37.609:E / AndroidRuntime(15241):在android.os.Looper.loop(Looper.java:146) 02-08 22:54:37.609:E / AndroidRuntime(15241):在android.app.ActivityThread.main(ActivityThread.java:5653) 02-08 22:54:37.609:E / AndroidRuntime(15241):at java.lang.reflect.Method.invokeNative(Native Method) 02-08 22:54:37.609:E / AndroidRuntime(15241):at java.lang.reflect.Method.invoke(Method.java:515) 02-08 22:54:37.609:E / AndroidRuntime(15241):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1291) 02-08 22:54:37.609:E / AndroidRuntime(15241):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107) 02-08 22:54:37.609:E / AndroidRuntime(15241):at dalvik.system.NativeStart.main(Native Method) 02-08 22:54:37.609:E / AndroidRuntime(15241):引起:java.lang.NullPointerException 02-08 22:54:37.609:E / AndroidRuntime(15241):at java.lang.String.indexOf(String.java:989) 02-08 22:54:37.609:E / AndroidRuntime(15241):at org.apache.cordova.CordovaWebView.loadUrlNow(CordovaWebView.java:471) 02-08 22:54:37.609:E / AndroidRuntime(15241):at org.apache.cordova.CordovaWebView.loadUrl(CordovaWebView.java:356) 02-08 22:54:37.609:E / AndroidRuntime(15241):at org.apache.cordova.CordovaWebView.handlePause(CordovaWebView.java:824) 02-08 22:54:37.609:E / AndroidRuntime(15241):at org.apache.cordova.DroidGap.onPause(DroidGap.java:649) 02-08 22:54:37.609:E / AndroidRuntime(15241):在android.app.Activity.performPause(Activity.java:5645) 02-08 22:54:37.609:E / AndroidRuntime(15241):在android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1239) 02-08 22:54:37.609:E / AndroidRuntime(15241):在android.app.ActivityThread.performPauseActivity(ActivityThread.java:3383) 02-08 22:54:37.609:E / AndroidRuntime(15241):... 12更多 02-08 22:54:39.119:I / System.out(15241):邮件数量= 0 02-08 22:54:39.559:I / Process(15241):发送信号。 PID:15241 SIG:9