2014-05-11 4 views
0

SMS가 응용 프로그램에서 수정되었을 때 사용자에게 알리는 간단한 응용 프로그램을 만듭니다. 콘텐츠 관찰자를 사용하고 있습니다. 내 응용 프로그램 꺼져 그래서 내 응용 프로그램은 꺼져 있고 그가 한 SMS 여기 SMS 응용 프로그램에서 수정 된 SMS가있는 경우 사용자에게 알립니다.

public class SMSObserver extends ContentObserver 
{ 
    SMSLogger smsLogger; 
    Context context; 

    public SMSObserver(SMSLogger smsLogger, Context context) { 

     super(new Handler()); 
     this.context=context; 
     this.smsLogger = smsLogger; 
    } 

    @Override 
    public void onChange(boolean selfChange) { 

     super.onChange(selfChange); 
     smsLogger.querySMS(context); 
    } 
} 




public class SMSLogger { 
    protected void querySMS(Context context) { 
     Uri uriSMS = Uri.parse("content://sms/"); 
     Cursor cur = context.getContentResolver().query(uriSMS, null, null, null, null); 
     /* cur.moveToNext(); // this will make it point to the first record, which is the last SMS sent 
     String body = cur.getString(cur.getColumnIndex("body")); //content of sms 
     String add = cur.getString(cur.getColumnIndex("address")); //phone num 
     String time = cur.getString(cur.getColumnIndex("date")); //date 
     String protocol = cur.getString(cur.getColumnIndex("protocol")); //protocol*/ 

     Toast.makeText(context, "Data Changed CHECK SMS" , Toast.LENGTH_SHORT).show(); 

    /*logging action HERE...*/ 
    } 
} 

내 코드이며, 여기

모든 등록 코드 수정 된 것을 notfication을 받아야 읽을 때 일부 사용자는 SMS를 표시하는 경우 아이디어 어디 사용자가 항상 SMS를 업데이트 할 때 알림을받을 수 있도록이 코드 줄을 추가 하시겠습니까?

Toast.makeText(this.getBaseContext(), "Data Changed CHECK SMS", Toast.LENGTH_SHORT).show(); 
     final Uri SMS_STATUS_URI = Uri.parse("content://sms"); 
     SMSLogger sl= new SMSLogger(); 

     SMSObserver smsSentObserver = new SMSObserver(sl, this.getBaseContext()); 
     getContentResolver().registerContentObserver(SMS_STATUS_URI, true, smsSentObserver); 
당신은 서비스를 만들고 당신이 어떤 incomming을 차단하기 위해 보장 다음은 시스템에 의해

private void startServiceAsForeground() { 
     Intent intent = new Intent(MyServiceService.this, targetActivity.class); 
     startForeground(ONGOING_NITIFICATION_ID, creatOngoingNotificationObject(getString(R.string.ongoing_message_ok), intent)); 

    } 

    private void updateServiceForegroundMessage(String message, Intent intent) { 
     startForeground(ONGOING_NITIFICATION_ID, creatAntiDistractionOngoingNotificationObject(message, intent)); 
    } 

    private android.app.Notification creatOngoingNotificationObject(String messageBody, Intent intent) { 
     NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(MyServiceService.this).setSmallIcon(R.drawable.ic_launcher_gray) 
       .setContentTitle(getString(R.string.app_name)).setAutoCancel(false).setOngoing(true).setContentText(messageBody); 
     // Intent intent = new Intent(MainService.this, intentClass); 
     intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); 
     PendingIntent pIntent = PendingIntent.getActivity(MyServiceService.this, 0, intent, 0); 
     mBuilder.setContentIntent(pIntent); 
     return mBuilder.build(); 
    } 

을 임의의 살해를 방지하고 서비스에 코드를 삽입하는 전경에 실행할 수 있도록 할 수 있습니다

답변

0

/나가는 메시지는 서비스가 항상 실행되고 사용자가이를 인식하므로 애플리케이션이 포 그라운드에 있지 않아도됩니다.