当我安装我的应用程序时,Anti-Virus告诉我该程序是有害的 我编写的程序在特定的时间间隔内在后台运行声音,这是我的代码:
public class Main2Activity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
boolean check_service;
int time = 59995;
Calendar calendar;
AlarmManager alarms;
PendingIntent recurringLl24;
Switch mySwitch;
String item;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.hide();
}
///////////////////////////////////////
Spinner spinner = (Spinner) findViewById(R.id.planets_spinner);
spinner.setOnItemSelectedListener(this);
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.planets_array, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
spinner.setAdapter(adapter);
/////////////////////////////////////////////
// final Intent i = new Intent(this, MyService.class);
mySwitch = (Switch) findViewById(R.id.switch1);
check_service = isMyServiceRunning(LifeLogService.class);
if (check_service == true) {
mySwitch.setChecked(true);
} else {
mySwitch.setChecked(false);
}
////////////////////////
calendar = Calendar.getInstance();
Intent ll24 = new Intent(Main2Activity.this, AlarmReceiverLifeLog.class);
recurringLl24 = PendingIntent.getBroadcast(Main2Activity.this, 0, ll24, PendingIntent.FLAG_CANCEL_CURRENT);
alarms = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
mySwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
// switchStatus.setText("Switch is currently ON");
try {
switch (time) {
case 59995:
alarms.setRepeating(AlarmManager.RTC_WAKEUP, 5000, 599995, recurringLl24); // Log repetition
// writeToFile(item,this);
break;
case 1799995:
alarms.setRepeating(AlarmManager.RTC_WAKEUP, 5000, AlarmManager.INTERVAL_HALF_HOUR, recurringLl24); // Log repetition
break;
case 3599995:
alarms.setRepeating(AlarmManager.RTC_WAKEUP, 5000, AlarmManager.INTERVAL_HOUR, recurringLl24); // Log repetition
break;
case 7199995:
alarms.setRepeating(AlarmManager.RTC_WAKEUP, 5000, 7199995, recurringLl24); // Log repetition
break;
case 90000:
alarms.setRepeating(AlarmManager.RTC_WAKEUP, 5000, 90000, recurringLl24); // Log repetition
break;
case 2700000:
alarms.setRepeating(AlarmManager.RTC_WAKEUP, 5000, 2700000, recurringLl24); // Log repetition
break;
}
// alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_FIFTEEN_MINUTES, alarmIntent);
Toast.makeText(Main2Activity.this, "بدا التسبيح ", Toast.LENGTH_LONG).show();
//mySwitch.setChecked(true);
} catch (Exception e) {
}
} else {
try {
//stopService(i);
alarms.cancel(recurringLl24);
Toast.makeText(Main2Activity.this, "توقف التسبيح", Toast.LENGTH_SHORT).show();
//mySwitch.setChecked(false);
} catch (Exception e) {
}
}
}
});
}
private boolean isMyServiceRunning(Class<?> serviceClass) {
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (serviceClass.getName().equals(service.service.getClassName())) {
return true;
}
}
return false;
}
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
// On selecting a spinner item
item = adapterView.getItemAtPosition(i).toString();
switch (item) {
case "10 MIN":
time = 59995;
alarms.cancel(recurringLl24);
mySwitch.setChecked(false);
Toast.makeText(Main2Activity.this, "توقف التسبيح", Toast.LENGTH_SHORT).show();
break;
case "30 MIN":
time = 1799995;
alarms.cancel(recurringLl24);
mySwitch.setChecked(false);
Toast.makeText(Main2Activity.this, "توقف التسبيح", Toast.LENGTH_SHORT).show();
break;
case "Hour":
time = 3599995;
alarms.cancel(recurringLl24);
mySwitch.setChecked(false);
Toast.makeText(Main2Activity.this, "توقف التسبيح", Toast.LENGTH_SHORT).show();
break;
case "2 Hour":
time = 7199995;
alarms.cancel(recurringLl24);
mySwitch.setChecked(false);
Toast.makeText(Main2Activity.this, "توقف التسبيح", Toast.LENGTH_SHORT).show();
break;
case "15 MIN":
time = 900000;
alarms.cancel(recurringLl24);
mySwitch.setChecked(false);
break;
case "45 MIN":
time = 2700000;
alarms.cancel(recurringLl24);
mySwitch.setChecked(false);
break;
}
// Showing selected spinner item
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
}
我的服务是
public class LifeLogService extends Service {
public LifeLogService() {
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
MediaPlayer objPlayer;
private NotificationManager mNotificationManager;
private int notificationID = 100;
private int numMessages = 0;
@Override
public int onStartCommand(Intent intent,int flags,int startId){
/* Invoking the default notification service */
final NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
mBuilder.setContentTitle("تسبيحه جديده");
mBuilder.setContentText("سبحان الله والحمد لله ولا اله الا الله");
mBuilder.setTicker("سبحان الله والحمد لله ولا اله الا الله");
mBuilder.setSmallIcon(R.drawable.ac);
/* Increase notification number every time a new notification arrives */
mBuilder.setNumber(++numMessages);
/* Creates an explicit intent for an Activity in your app */
Intent resultIntent = new Intent(this, NotificationView.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(NotificationView.class);
/* Adds the Intent that starts the Activity to the top of the stack */
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent( 0, PendingIntent.FLAG_UPDATE_CURRENT );
mBuilder.setContentIntent(resultPendingIntent);
////////////////////////
///////////////////////////////////////
objPlayer = MediaPlayer.create(this,R.raw.sphan_allah);
Runnable r=new Runnable() {
@Override
public void run() {
{synchronized (this){
try {
//wakeLock.acquire();
objPlayer.start();
mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
/* notificationID allows you to update the notification later on. */
mNotificationManager.notify(notificationID, mBuilder.build());
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
};
Thread bucky=new Thread(r);
bucky.start();
return Service.START_STICKY;
}
}
我的BroadcastReceiver是
public class AlarmReceiverLifeLog extends BroadcastReceiver {
public AlarmReceiverLifeLog() {
}
private static final String TAG = "LL24";
static Context context;
@Override
public void onReceive(Context context, Intent intent) {
Log.v(TAG, "Alarm for LifeLog...");
Intent ll24Service = new Intent(context, LifeLogService.class);
context.startService(ll24Service);
}
}
我的NotificationView是:
public class NotificationView extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notification_view);
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.hide();
}
}
}