我的应用程序有关安排短信的问题。如果我按时间顺序安排短信,例如第一个短信在12:00,第二个在13:00,我的应用程序工作。但如果我先在13:00然后在12:00安排短信,我的应用程序就无法运行。
我使用排序alghoritm,但它不起作用!
我不知道为什么。
我告诉你我的代码。
提前致谢!
主要活动:
public class MainActivity extends Activity {
private static final int PERMS_REQUEST_CODE = 123;
private static final int REQUEST_CODE = 1;
TextView textViewTime;
TimePicker timePicker;
Button button;
String name2=null;
String sms2=null;
long tempo2;
int hour;
int minutee;
//int hour;
//int minute;
final int TIME_DIALOG_ID = 999;
public String sPhone, sSms;
public EditText etPhone, etSms;
public Button bStart, bCancel, bTimeSelect, bPhone;
ArrayList<Sms> dataSms;
ListView listView;
public static CustomAdapter adapter;
private static MainActivity ins;
EditText et1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ins = this;
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.SEND_SMS}, 1);
listView = (ListView) findViewById(R.id.listView1);
/*listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> a, View v, int position,
long id) {
Toast.makeText(MainActivity.this, "Item has been added into your list", Toast.LENGTH_LONG)
.show();
//dataSms.remove(position);
//adapter.notifyDataSetChanged();
Sms dataModel= dataSms.get(position);
//dataModel.setTesto();
}
});*/
dataSms = new ArrayList<Sms>();
adapter = new CustomAdapter(dataSms, getApplicationContext());
listView.setAdapter(adapter);
//Button b=(Button) findViewById(R.id.button1);
//Button button=(Button) findViewById(R.id.button);
bStart = (Button) findViewById(R.id.bStart);
et1=(EditText)findViewById(R.id.editText1);
//bCancel = (Button)findViewById(R.id.bCancel);
//bTimeSelect = (Button) findViewById(R.id.bTime);
bPhone = (Button) findViewById(R.id.bCPhone);
bStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
/* if (hasPermissions()) {
// our app has permissions.
scheduleAlarm(v);
} else {
//our app doesn't have permissions, So i m requesting permissions.
requestPerms();
}*/
try {
scheduleAlarm(v);
} catch (ParseException e) {
e.printStackTrace();
}
}
});
et1.setOnClickListener(new View.OnClickListener() {
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@Override
public void onClick(View v) {
DialogFragment newFragment = new TimePickerFragment();
newFragment.show(getFragmentManager(), "TimePicker");
}
});
/* bTimeSelect.setOnClickListener(new View.OnClickListener() {
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@Override
public void onClick(View v) {
DialogFragment newFragment = new TimePickerFragment();
newFragment.show(getFragmentManager(), "TimePicker");
}
});*/
//};
bPhone.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Uri uri = Uri.parse("content://contacts");
Intent intent = new Intent(Intent.ACTION_PICK, uri);
intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
startActivityForResult(intent, REQUEST_CODE);
}
});
}
public static MainActivity getInstace(){
return ins;
}
public void updateTheTextView() {
MainActivity.this.runOnUiThread(new Runnable() {
public void run() {
// for(int i=0;i<adapter.getCount();i++)
// {
dataSms.remove(0);
adapter.notifyDataSetChanged();
// }
adapter.notifyDataSetChanged();
}
});
//adapter.notifyDataSetChanged();
//Toast.makeText(this, "COUNT DELETE: "+adapter.getCount(), Toast.LENGTH_LONG).show();
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@SuppressLint("ValidFragment")
public class TimePickerFragment extends DialogFragment implements TimePickerDialog.OnTimeSetListener {
//TimePicker timePicker;
//int hour;
//int minutee;
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
//Use the current time as the default values for the time picker
final Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
//Create and return a new instance of TimePickerDialog
return new TimePickerDialog(getActivity(), this, hour, minute,
DateFormat.is24HourFormat(getActivity()));
}
//onTimeSet() callback method
@TargetApi(Build.VERSION_CODES.M)
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
hour = hourOfDay;
minutee = minute;
et1.setText( hour + ":" + minutee);
}
}
@TargetApi(Build.VERSION_CODES.M)
public void scheduleAlarm(View V) throws ParseException {
EditText et1 = (EditText) findViewById(R.id.etPhone);
EditText et2 = (EditText) findViewById(R.id.etSms);
String name = et1.getText().toString();
String sms = et2.getText().toString();
//PendingIntent pendingIntent;
//Long time= new GregorianCalendar().getTimeInMillis()+24*60*60*1000;
//Long time = new GregorianCalendar().getTimeInMillis() + 10000;
//Long time= System.currentTimeMillis() + 10000;
/*Intent intentAlarm = new Intent(this, AlarmReceiver.class);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Bundle bundle = new Bundle();
bundle.putCharSequence("Number", name);
bundle.putCharSequence("Message", sms);*/
//intentAlarm.putExtras(bundle);
final Calendar c = Calendar.getInstance();
c.setTimeInMillis(System.currentTimeMillis());
c.set(Calendar.HOUR_OF_DAY, hour);
c.set(Calendar.MINUTE, minutee);
long tempo=c.getTimeInMillis();
//Toast.makeText(this, "TEMPO: " + tempo, Toast.LENGTH_LONG).show();
//for(int i=0;i<dataSms.size();i++) {
adapter.add(new Sms(name, sms, tempo));
// Sms dataModel= dataSms.get(i);
// adapter.sortByNameAsc();
//Toast.makeText(this, "NOME: " + dataModel.getNome() +"TESTO "+dataModel.getTesto(), Toast.LENGTH_LONG).show();
//}
Toast.makeText(this, "COUNT: " + adapter.getCount(), Toast.LENGTH_LONG).show();
adapter.sortByNameAsc();
for(int i=0;i<dataSms.size();i++) {
//adapter.sortByNameAsc();
Sms dataModel= dataSms.get(i);
name2=dataModel.getNome();
sms2=dataModel.getTesto();
Intent intentAlarm = new Intent(this, AlarmReceiver.class);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
// Bundle bundle = new Bundle();
// bundle.putCharSequence("Number", name);
// bundle.putCharSequence("Message", sms);
/*
Sms dataModel= dataSms.get(i);
name2=dataModel.getNome();
sms2=dataModel.getTesto();*/
//tempo2=dataModel.getTempo();
Bundle bundle = new Bundle();
bundle.putCharSequence("Number", name2);
bundle.putCharSequence("Message", sms2);
intentAlarm.putExtras(bundle);
alarmManager.set(AlarmManager.RTC_WAKEUP, dataModel.getTempo(), PendingIntent.getBroadcast(this, i, intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT));
}
//alarmManager.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), PendingIntent.getBroadcast(this, 1, intentAlarm.putExtras(bundle), PendingIntent.FLAG_UPDATE_CURRENT));
Toast.makeText(this, "Messaggio programmato!", Toast.LENGTH_LONG).show();
}
}
CUSTOM ADAPTER:
public class CustomAdapter extends ArrayAdapter<Sms> {
public static CustomAdapter adapter;
private ArrayList<Sms> dataSms;
Context mContext;
Sms sms2;
Bundle getBundle = null;
//ArrayList<Location> data = new ArrayList<Location>();
// View lookup cache
private static class ViewHolder {
TextView txtNome;
TextView txtTesto;
TextView txtOre;
TextView txtMinuti;
}
public CustomAdapter(ArrayList<Sms> data, Context context) {
super(context, R.layout.row_item, data);
this.dataSms = data;
this.mContext=context;
}
public void sortByNameAsc() {
Comparator<Sms> comparator = new Comparator<Sms>() {
@Override
public int compare(Sms object1, Sms object2) {
//return (int) (object1.getTempo() - object2.getTempo());
return Long.valueOf(object1.getTempo()).compareTo(Long.valueOf(object2.getTempo()));
//return Double.compare(object1.getPrice(), object2.getPrice());
//return compare(object1.getTempo() - object2.getTempo());
}
};
Collections.sort(dataSms, comparator);
notifyDataSetChanged();
}
/* @Override
public void onClick(View v) {
int position=(Integer) v.getTag();
Object object= getItem(position);
DataModel dataModel=(DataModel)object;
switch (v.getId())
{
case R.id.item_info:
Snackbar.make(v, "Release date " +dataModel.getFeature(), Snackbar.LENGTH_LONG)
.setAction("No action", null).show();
break;
}
}*/
//private int lastPosition = -1;
//dataSms = new ArrayList<Sms>();
//adapter = new CustomAdapter(dataSms, context);
@Override
public int getCount() {
return dataSms.size();
}
/* @Override
public Object getItem(int pos) {
return dataSms.get(pos);
}*/
@Override
public long getItemId(int position) {
return position;
}
// @Override
public long getTime(long c,int position) {
return c;
}
@Override
public void add(Sms sms) {
dataSms.add(sms);
notifyDataSetChanged();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Get the data item for this position
Sms sms = getItem(position);
// Check if an existing view is being reused, otherwise inflate the view
ViewHolder viewHolder; // view lookup cache stored in tag
final View result;
if (convertView == null) {
viewHolder = new ViewHolder();
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.row_item, parent, false);
viewHolder.txtNome = (TextView) convertView.findViewById(R.id.nome);
viewHolder.txtTesto = (TextView) convertView.findViewById(R.id.testo);
viewHolder.txtOre = (TextView) convertView.findViewById(R.id.ore);
viewHolder.txtMinuti = (TextView) convertView.findViewById(R.id.minuti);
result=convertView;
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
result=convertView;
}
/*Animation animation = AnimationUtils.loadAnimation(mContext, (position > lastPosition) ? R.anim.up_from_bottom : R.anim.down_from_top);
result.startAnimation(animation);*/
// lastPosition = position;
viewHolder.txtNome.setText(sms.getNome());
viewHolder.txtTesto.setText(sms.getTesto());
// viewHolder.txtTempo.setText(String.valueOf(sms.getTempo()));
//viewHolder.txtOre.setText(String.valueOf(sms.getOre())+":");
// viewHolder.txtMinuti.setText(String.valueOf(sms.getMinuti()));
/*viewHolder.info.setOnClickListener(this);
viewHolder.info.setTag(position);*/
// Return the completed view to render on screen
return convertView;
}
}
模特课程:
public class Sms {
long tempo;
String nome;
String testo;
public Sms(String nome, String testo, long tempo) {
this.nome=nome;
this.testo=testo;
this.tempo=tempo;
//this.ore=ore;
// this.minuti=minuti;
}
public void setNome(String nome) {
this.nome = nome;
}
//int ore;
//public int getMinuti() {
// return minuti;
// }
//public void setMinuti(int minuti) {
// this.minuti = minuti;
// }
// public int getOre() {
// return ore;
// }
// public void setOre(int ore) {
// this.ore = ore;
// }
//int minuti;
public void setTesto(String testo) {
this.testo = testo;
}
//int indice;
public long getTempo() {
return tempo;
}
public void setTempo(long tempo) {
this.tempo = tempo;
}
public String getNome() {
return nome;
}
public String getTesto() {
return testo;
}
}