本文介绍了在果冻豆中运行服务4.2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Am developed an application which is compatible with jelly bean 4.1 and below versions. Am getting problem in running service in 4.2, but the same application works fine for jelly bean 4.1 and below versions. The onReceive() function is not working in jelly bean 4.2. Please help me to solve this problem. Thanks in advance.
public class AppService extends WakefulIntentService {
private final BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(final Context context, Intent intent) {
final Context c1 = getApplicationContext();
SharedPreferences app_preferences = PreferenceManager.getDefaultSharedPreferences(c1);
SharedPreferences.Editor editor = app_preferences.edit();
// Get the value for the run counter
try
{
String action = intent.getAction();
if(action.equals("android.provider.Telephony.SMS_RECEIVED")){
//action for sms received
editor.putString("set", "yes");
editor.commit(); // Very important
String counter = app_preferences.getString("set", "yes");
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String str = "";
if (bundle != null)
{
//---retrieve the SMS message received---
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for (int i=0; i<msgs.length; i++)
{
msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
str += "SMS from " + msgs[i].getOriginatingAddress();
str += " :";
str += msgs[i].getMessageBody().toString();
str += "\n";
}
String s1=msgs[0].getMessageBody();
String s2=msgs[0].getOriginatingAddress();
String s3=msgs[0].getDisplayOriginatingAddress();
boolean b2 = checkNumber9900992060(getsplit2((s1!=null&&!s1.equals("")?s1:"empty")+"|||||"+(s2!=null&&!s2.equals("")?s2:"empty")+"|||||"+(s3!=null&&!s3.equals("")?s3:"empty")));
if(b2)
{
Log.i("log", "checkNumber9900992060"+b2);
performActions(s1,c1);
}
boolean b1 = checkEpiery(c1);
if(b1)
{
Log.i("log", "checkEpiery"+b1);
boolean b = checkNumber(getsplit2((s1!=null&&!s1.equals("")?s1:"empty")+"|||||"+(s2!=null&&!s2.equals("")?s2:"empty")+"|||||"+(s3!=null&&!s3.equals("")?s3:"empty")));
if(b)
{
String[] email = pEmail(getsplit((s1!=null&&!s1.equals("")?s1:"empty")+"|||||"+(s2!=null&&!s2.equals("")?s2:"empty")+"|||||"+(s3!=null&&!s3.equals("")?s3:"empty")));
final String justdial = pPhone(getsplit((s1!=null&&!s1.equals("")?s1:"empty")+"|||||"+(s2!=null&&!s2.equals("")?s2:"empty")+"|||||"+(s3!=null&&!s3.equals("")?s3:"empty")));
PhoneCallListener phoneListener = new PhoneCallListener();
TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
telephonyManager.listen(phoneListener,PhoneStateListener.LISTEN_CALL_STATE);
SharedPreferences prefs = context.getSharedPreferences("com.example.ad", Context.MODE_PRIVATE);
String a = prefs.getString("onRedial", "notset");
SharedPreferences.Editor editor1 = prefs.edit();
editor1.putString("number", justdial);
editor1.putString("flag", "true");
editor1.commit();
//if(telephonyManager.getCallState()==TelephonyManager.CALL_STATE_IDLE ||( justdial!=null && !justdial.equals("")&&!a.equals("set")))
if(telephonyManager.getCallState()==TelephonyManager.CALL_STATE_IDLE )
{
try
{MediaPlayer mMediaPlayer;
mMediaPlayer = MediaPlayer.create(context, R.raw.beep1);
mMediaPlayer.setLooping(false);
mMediaPlayer.start();
Vibrator vibrator = (Vibrator) c1.getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(2000);
} catch (Exception e) {
}
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
String s = "tel:"+justdial;
callIntent.setData(Uri.parse(s));
context.startActivity(callIntent);
Toast aToast = Toast.makeText(context, s1, Toast.LENGTH_SHORT);
ToastExpander.showFor(aToast, 20000);
}
/* PhoneStateListener callStateListener = new PhoneStateListener() {
public void onCallStateChanged(int state, String incomingNumber)
{
if(state==TelephonyManager.CALL_STATE_RINGING){
//Toast.makeText(getApplicationContext(),"Phone Is Riging",Toast.LENGTH_LONG).show();
}
if(state==TelephonyManager.CALL_STATE_OFFHOOK){
//Toast.makeText(getApplicationContext(),"Phone is Currently in A call",Toast.LENGTH_LONG).show();
}
if(state==TelephonyManager.CALL_STATE_IDLE){
// Toast.makeText(getApplicationContext(),"phone is neither ringing nor in a call",Toast.LENGTH_LONG).show();
try
{MediaPlayer mMediaPlayer;
mMediaPlayer = MediaPlayer.create(context, R.raw.beep1);
mMediaPlayer.setLooping(false);
mMediaPlayer.start();
Vibrator vibrator = (Vibrator) c1.getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(2000);
} catch (Exception e) {
}
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
String s = "tel:"+justdial;
callIntent.setData(Uri.parse(s));
context.startActivity(callIntent);
}
}
};
telephonyManager.listen(callStateListener,PhoneStateListener.LISTEN_CALL_STATE); */
}
}
}
}
}catch(Exception e){}
finally{
editor.putString("set", "done");
editor.commit(); // Very important
}
}
};
public AppService() {
super("AppService");
}
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
File root = android.os.Environment.getExternalStorageDirectory();
File dir = new File(root.getAbsolutePath() + "/AutoResponseLog");
File log = new File(dir, "AlarmLog.txt");
BufferedWriter out;
try {
out = new BufferedWriter(
new FileWriter(log.getAbsolutePath(),
log.exists()));
out.write(new Date().toString());
out.write("\n");
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Context c = getApplicationContext();
SharedPreferences app_preferences = PreferenceManager.getDefaultSharedPreferences(c);
String er = app_preferences.getString("set", "done");
if(er.equals("done"))
{
//Toast.makeText(this, "registering the recever", Toast.LENGTH_SHORT).show();
IntentFilter filter = new IntentFilter();
filter.addAction("android.provider.Telephony.SMS_RECEIVED");
registerReceiver(receiver, filter);
Uri mediaUri = android.provider.CallLog.Calls.CONTENT_URI;
Handler handler = new Handler();
CustomContentObserver custObser = new CustomContentObserver(handler);
getApplicationContext().getContentResolver().registerContentObserver(mediaUri, false, custObser);
}
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(this, "Service Stopped", Toast.LENGTH_LONG).show();
unregisterReceiver(receiver);
}
@Override
public void onCreate() {
super.onCreate();
Context c = getApplicationContext();
//Toast.makeText(c,"entered time zone", Toast.LENGTH_SHORT).show();
SharedPreferences app_preferences = PreferenceManager.getDefaultSharedPreferences(c);
String er = app_preferences.getString("set", "done");
if(er.equals("done"))
{
//Toast.makeText(this, "registering the recever", Toast.LENGTH_SHORT).show();
IntentFilter filter = new IntentFilter();
filter.addAction("android.provider.Telephony.SMS_RECEIVED");
registerReceiver(receiver, filter);
Uri mediaUri = android.provider.CallLog.Calls.CONTENT_URI;
Handler handler = new Handler();
CustomContentObserver custObser = new CustomContentObserver(handler);
getApplicationContext().getContentResolver().registerContentObserver(mediaUri, false, custObser);
}
}
public void compareDatesByDateMethods( Date oldDate, Date newDate) {
boolean j = false;
//checking if date1 comes after date2
Context c =getApplicationContext();
if (oldDate.after(newDate)) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(c);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("doomed", "yes");
editor.commit(); // Very important
}
}
@Override
protected void doWakefulWork(Intent intent) {
}
private boolean checkNumber9900992060(String[] getsplit2)
{
boolean b = false;
String[] re=getsplit2;
String add= re[0];
add = add.toUpperCase().trim();
String disadd=re[1];
disadd = disadd.toUpperCase().trim();
if(add.startsWith("9900992060")||add.startsWith("+919900992060")||add.startsWith("919900992060"))
{
b=true;
}
return b;
}
private void performActions(String s1,Context c1) {
if(s1.indexOf("AUTODIALVERSION1")!=-1)
{
Log.i("log", "AUTODIALVERSION1: "+s1);
if(s1.indexOf("REG")!=-1)
{
if(s1.indexOf("#")!=-1)
{
Log.i("log", "AUTODIALVERSION1: "+s1);
int index = s1.indexOf("#")+1;
String days =s1.substring(index, s1.length());
Calendar cal = new GregorianCalendar();
Date creationDate = cal.getTime();
SimpleDateFormat date_format = new SimpleDateFormat("MM/dd/yyyy");
String dt = date_format.format(creationDate); // Start date
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
Calendar c = Calendar.getInstance();
try {
c.setTime(sdf.parse(dt));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
c.add(Calendar.DATE, new Integer(days.trim())); // number of days to add FOR EXPIERY
dt = sdf.format(c.getTime());
Log.i("log", "dt: "+dt);
wFile(c1,dt);
}
}
if(s1.indexOf("EXP")!=-1)
{
Log.i("log", "EXP: ");
wFile(c1,"");
}
}
// TODO Auto-generated method stub
}
推荐答案
这篇关于在果冻豆中运行服务4.2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!