本文介绍了额外的不经过意图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想Alarm_no传递给alarm_Time.java,当报警开始,我可以取消它,并通过意图得到Alarm_no
警报被取消,但Alarm_no没有通过,而pending_intent是空的,我不知道为什么,以及如何解决这个问题?
MainActivity2
公共类MainActivity2扩展AppCompatActivity {
TextView的update_txt;
TextView的info_txt;
上下文语境;
@覆盖
保护无效的onCreate(捆绑savedInstanceState){ super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main2);
工具条工具栏=(栏)findViewById(R.id.toolbar);
setSupportActionBar(工具栏); ///
this.context =这一点; update_txt =(的TextView)findViewById(R.id.update_txt);
info_txt =(的TextView)findViewById(R.id.info_txt); 最终诠释[] H = {23,23};
最终诠释[] M = {21,22};
最终诠释长度= h.length; 最终意图[] =意向意向书新[长度]
最后的日历[] = CAL新的日历[长度]
最后AlarmManager [] = alarmManager新AlarmManager [长度]
最终的PendingIntent [] = pending_intent新的PendingIntent [长度] ; 的for(int i = 0; I<长度;我++){
CAL [I] = Calendar.getInstance();
CAL [I] .SET(Calendar.HOUR_OF_DAY,H [我]);
CAL [I] .SET(Calendar.MINUTE,M []);
如果(CAL [I] .getTimeInMillis()< System.currentTimeMillis的())
{CAL [I]。新增(Calendar.DAY_OF_MONTH,1); } 意图[I] =新意图(this.context,AlarmReceiver.class);
alarmManager [I] =(AlarmManager)getSystemService(ALARM_SERVICE); }
按钮ALARM_ON =(按钮)findViewById(R.id.alarm_on);
alarm_on.setOnClickListener(新View.OnClickListener(){
@覆盖
公共无效的onClick(视图v){ 串h1_string =将String.valueOf(H [0]);
串m1_string =将String.valueOf(M [0]);
的for(int i = 0; I<长度;我++){
set_alarm_text(报警吧!+ h1_string +:+ m1_string); 意图[I] .putExtra(额外,报警);
意图[I] .putExtra(Alarm_no我);
pending_intent [I] = PendingIntent.getBroadcast(MainActivity2.this,I,意图由[i],PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager [I] .SET(AlarmManager.RTC_WAKEUP,CAL [I] .getTimeInMillis(),pending_intent [I]); }
}
}); // intialize alarm_off按钮
按钮alarm_off =(按钮)findViewById(R.id.alarm_off);
//创建点击听者结束报警
alarm_off.setOnClickListener(新View.OnClickListener(){
@覆盖
公共无效的onClick(视图v){
捆绑额外= getIntent()getExtras()。
INT alarm_No = -1;
如果(临时演员!= NULL){
alarm_No = extras.getInt(alarm_No2);
} Log.v(帮助,将String.valueOf(alarm_No)); //方法update_txt txtbox改变
set_alarm_text(报警了!);
//取消报警
alarmManager [0] .cancel(pending_intent [0]);
//把多余的串入my_intent
//告诉您pressed报警关闭按钮时钟
意图[0] .putExtra(额外,报警关);
//停止铃声
sendBroadcast(意向[0]);
}
});
}私人无效set_alarm_text(字符串输出){
update_txt.setText(输出);
}私人无效set_info_text(字符串输出){
info_txt.setText(输出);
}
@覆盖
公共布尔onCreateOptionsMenu(菜单菜单){
。getMenuInflater()膨胀(R.menu.menu_main,菜单);
返回true;
}
@覆盖
公共布尔onOptionsItemSelected(菜单项项){ INT ID = item.getItemId();
如果(ID == R.id.action_settings){
返回true;
}
返回super.onOptionsItemSelected(项目);
}}
AlarmReceiver
公共类AlarmReceiver扩展广播接收器{
@覆盖
公共无效的onReceive(上下文的背景下,意图意图){
Log.e(我们是在接收器,耶); //获取来自意图&GT额外的字符串;>>报警关 - get_ur_string =额外//报警
字符串get_ur_string = intent.getExtras()的getString(额外)。
INT否= intent.getIntExtra(Alarm_no, - 1);
// Log.e(关键是什么?,get_ur_string);
//创建一个意图铃声服务
意图service_intent =新意图(背景下,RingtonePlayingService.class); //从主要活动传递到额外的铃声播放服务
service_intent.putExtra(额外,get_ur_string);
service_intent.putExtra(Alarm_no,否); //启动铃声服务
context.startService(service_intent);}}
RingtonePlayingService
公共类RingtonePlayingService延伸服务{MediaPlayer的media_song;
INT startId;
布尔isRunning;@Nullable
@覆盖
公众的IBinder onBind(意向意图){
返回null;
}公众诠释onStartCommand(意向意图,诠释旗帜,INT startId)
{
Log.i(本地服务,收到起始ID+ startId +:+意向);
//获取额外的字符串值
字符串状态= intent.getExtras()的getString(额外)。
INT否= intent.getIntExtra(Alarm_no,-2);
Log.e(铃声状态:额外的,状态);
//通知
//设置通知服务
NotificationManager notify_manager =(NotificationManager)
getSystemService(NOTIFICATION_SERVICE);
//设置一个进入的主要活动意图
意图intent_main_activity =新意图(this.getApplicationContext(),alarm_Time.class);
intent.putExtra(额外,报警);
intent.putExtra(Alarm_no,否);
//设置一个悬而未决的意图
的PendingIntent pending_intent_main_activity = PendingIntent.getActivity(这一点,不,
intent_main_activity,PendingIntent.FLAG_UPDATE_CURRENT);
//使通知参数
通知notification_popup =新Notification.Builder(本)
.setContentTitle(一个警报响起时!)
.setContentText(点击我!)
.setSmallIcon(R.drawable.image)
.setContentIntent(pending_intent_main_activity)
.setAutoCancel(真)
。建立();
//设置通知atart命令
notify_manager.notify(0,notification_popup);
//这个转换的额外字符串从意向开始的ID,值0或1
断言状态!= NULL;
开关(州){
案报警:
startId = 1;
INT alarm_num = intent.getExtras()调用getInt(否)。
打破;
案报警关:
startId = 0;
Log.e(开始ID为状态);
打破;
默认:
startId = 0;
打破;
}
//如果其他statments
//如果没有音乐播放,而且用户pressing报警的
//音乐开始播放
如果(this.isRunning&安培;!&安培; startId == 1){
Log.e(没有音乐,和u要启动);
//创建媒体播放器的瞬间
media_song = MediaPlayer.create(这一点,R.raw.tone);
//启动铃声
media_song.start();
this.isRunning = TRUE;
this.startId = 0; }
//如果有音乐播放,而且用户pressing报警关
//音乐停止播放
否则,如果(this.isRunning&安培;&安培; startId == 0){
Log.e(有乐,和U希望停止); //停止铃声
media_song.stop();
media_song.reset();
this.isRunning = FALSE;
this.startId = 0; }
返回START_NOT_STICKY;
}@覆盖
公共无效的onDestroy(){
//告诉我们停止用户
Log.e(关于销毁称为,ただ」); super.onDestroy();
this.isRunning = FALSE;}}
ALARM_TIME
公共类ALARM_TIME扩展AppCompatActivity {
@覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_alarm__time);
最终的工具条工具栏=(栏)findViewById(R.id.toolbar);
setSupportActionBar(工具栏); 最后FloatingActionButton FAB =(FloatingActionButton)findViewById(R.id.fab);
fab.setOnClickListener(新View.OnClickListener(){
@覆盖
公共无效的onClick(查看视图){
Snackbar.make(看来,用自己的行动替换,Snackbar.LENGTH_LONG)
.setAction(动作,NULL).show();
}
});
//最终意图的意图;
最后AlarmManager alarmManager;
最终的PendingIntent pending_intent;
alarmManager =(AlarmManager)getSystemService(ALARM_SERVICE);
//意图=新意图(this.context,RingtonePlayingService.class);
最终意向意图=新意图(这一点,AlarmReceiver.class);
INT否= getIntent()getIntExtra(Alarm_no,-20)。
INT 1号= intent.getIntExtra(Alarm_no, - 5);
Log.v(看,将String.valueOf(否));
Log.v(see1,将String.valueOf(1号)); pending_intent = PendingIntent.getBroadcast(alarm_Time.this,不,意图,PendingIntent.FLAG_NO_CREATE);
如果(pending_intent == NULL){
Log.v(帮助,的PendingIntent为空);
}
其他{
Log.v(帮助,的PendingIntent不为空);
}
// intialize alarm_off按钮
按钮alarm_off =(按钮)findViewById(R.id.alarm_off);
//创建点击听者结束报警
//最终意图finalIntent =意图;
alarm_off.setOnClickListener(新View.OnClickListener(){
@覆盖
公共无效的onClick(视图v){
//方法update_txt txtbox改变
// set_alarm_text(报警了!);
//取消报警
alarmManager.cancel(pending_intent);
//把多余的串入my_intent
//告诉您pressed报警关闭按钮时钟
intent.putExtra(额外,报警关);
//停止铃声
sendBroadcast(意向);
}
});
}}
解决方案
这是在code你的服务的 onStartCommand()
:
//设置一个进入的主要活动意图
意图intent_main_activity =新意图(this.getApplicationContext()
alarm_Time.class);
intent.putExtra(额外,报警);
intent.putExtra(Alarm_no,否);
您想要把群众演员,在 intent_main_activity
而不是意图
。
I'm trying to pass Alarm_no to alarm_Time.java, and when alarm is started I can cancel it and get the Alarm_no through intentthe alarm is canceled but the Alarm_no doesn't pass, and the pending_intent is null, I don't know why and how to fix that?
MainActivity2
public class MainActivity2 extends AppCompatActivity {
TextView update_txt;
TextView info_txt;
Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar); ///
this.context = this ;
update_txt = (TextView) findViewById(R.id.update_txt);
info_txt = (TextView) findViewById(R.id.info_txt);
final int[] h = {23, 23};
final int[] m = {21, 22};
final int length = h.length;
final Intent[] intent = new Intent[length];
final Calendar[] cal = new Calendar[length];
final AlarmManager[] alarmManager = new AlarmManager[length];
final PendingIntent[] pending_intent = new PendingIntent[length]; ;
for(int i =0; i<length; i++) {
cal[i] = Calendar.getInstance();
cal[i].set(Calendar.HOUR_OF_DAY, h[i]);
cal[i].set(Calendar.MINUTE, m[i]);
if (cal[i].getTimeInMillis() < System.currentTimeMillis())
{ cal[i].add(Calendar.DAY_OF_MONTH, 1); }
intent[i] = new Intent(this.context, AlarmReceiver.class);
alarmManager[i] = (AlarmManager) getSystemService(ALARM_SERVICE);
}
Button alarm_on = (Button) findViewById(R.id.alarm_on);
alarm_on.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String h1_string = String.valueOf(h[0]);
String m1_string = String.valueOf(m[0]);
for(int i =0; i<length; i++) {
set_alarm_text("Alarm on!" + h1_string + ":" + m1_string);
intent[i].putExtra("extra", "alarm on");
intent[i].putExtra("Alarm_no", i);
pending_intent[i] = PendingIntent.getBroadcast(MainActivity2.this, i, intent[i], PendingIntent.FLAG_UPDATE_CURRENT );
alarmManager[i].set(AlarmManager.RTC_WAKEUP, cal[i].getTimeInMillis(), pending_intent[i]);
}
}
});
//intialize alarm_off button
Button alarm_off = (Button) findViewById(R.id.alarm_off);
// create on click listner to end alarm
alarm_off.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Bundle extras = getIntent().getExtras();
int alarm_No = -1;
if (extras != null) {
alarm_No = extras.getInt("alarm_No2");
}
Log.v("help", String.valueOf(alarm_No));
//method that change update_txt txtbox
set_alarm_text("Alarm off!");
//cancel the alarm
alarmManager[0].cancel(pending_intent[0]);
// put extra string into my_intent
// tells the clock that you pressed the "alarm off" button
intent[0].putExtra("extra", "alarm off");
// stop the ringtone
sendBroadcast(intent[0]);
}
});
}
private void set_alarm_text(String output) {
update_txt.setText(output);
}
private void set_info_text(String output) {
info_txt.setText(output);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}}
AlarmReceiver
public class AlarmReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
Log.e("We are in the receiver", "Yay");
//fetch extra strings from the intent >>> get_ur_string =extra // alarm on - alarm off
String get_ur_string = intent.getExtras().getString("extra");
int No = intent.getIntExtra("Alarm_no",-1);
// Log.e("What is the key? ", get_ur_string);
//create an intent to the ringtone service
Intent service_intent = new Intent(context, RingtonePlayingService.class);
// pass the extra from Main Activity to Ringtone Playing Service
service_intent.putExtra("extra", get_ur_string);
service_intent.putExtra("Alarm_no", No);
//start the ringtone services
context.startService(service_intent);
}}
RingtonePlayingService
public class RingtonePlayingService extends Service {
MediaPlayer media_song;
int startId;
boolean isRunning;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
public int onStartCommand(Intent intent, int flags, int startId)
{
Log.i("LocalService", "Received start id " + startId +": " + intent);
// fetch the extra string values
String state = intent.getExtras().getString("extra");
int No = intent.getIntExtra("Alarm_no", -2);
Log.e("Ringtone state:extra", state);
// notification
// set up the notification service
NotificationManager notify_manager = (NotificationManager)
getSystemService(NOTIFICATION_SERVICE);
// set up an intent that goes to the Main Activity
Intent intent_main_activity = new Intent(this.getApplicationContext(), alarm_Time.class);
intent.putExtra("extra", "alarm on");
intent.putExtra("Alarm_no", No);
// set up a pending intent
PendingIntent pending_intent_main_activity = PendingIntent.getActivity(this, No,
intent_main_activity, PendingIntent.FLAG_UPDATE_CURRENT);
// make the notification parameters
Notification notification_popup = new Notification.Builder(this)
.setContentTitle("An alarm is going off!")
.setContentText("Click me!")
.setSmallIcon(R.drawable.image)
.setContentIntent(pending_intent_main_activity)
.setAutoCancel(true)
.build();
// set up notification atart command
notify_manager.notify(0, notification_popup);
// this converts the extra strings from the intent to start IDs, values 0 or 1
assert state != null;
switch (state) {
case "alarm on":
startId = 1;
int alarm_num = intent.getExtras().getInt("No");
break;
case "alarm off":
startId = 0;
Log.e("Start ID is", state);
break;
default:
startId = 0;
break;
}
// if else statments
// if there is no music playing, and the user pressing "alarm on"
// music should start playing
if( !this.isRunning && startId == 1 ){
Log.e("there is no music","and u want start");
// create an instant of the media player
media_song = MediaPlayer.create(this, R.raw.tone);
// start the ringtone
media_song.start();
this.isRunning = true;
this.startId = 0;
}
// if there is music playing, and the user pressing "alarm off"
// music should stop playing
else if( this.isRunning && startId == 0 ){
Log.e("there is music", "and u want stop");
// stop the ringtone
media_song.stop();
media_song.reset();
this.isRunning = false;
this.startId = 0;
}
return START_NOT_STICKY;
}
@Override
public void onDestroy() {
//tell the user we stopped
Log.e("on Destroy called", "ta da");
super.onDestroy();
this.isRunning = false;
}}
alarm_Time
public class alarm_Time extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_alarm__time);
final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
final FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
// final Intent intent ;
final AlarmManager alarmManager ;
final PendingIntent pending_intent ;
alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
// intent = new Intent(this.context, RingtonePlayingService.class);
final Intent intent = new Intent(this, AlarmReceiver.class);
int No = getIntent().getIntExtra("Alarm_no", -20);
int No1 = intent.getIntExtra("Alarm_no",-5);
Log.v("see", String.valueOf(No));
Log.v("see1", String.valueOf(No1));
pending_intent = PendingIntent.getBroadcast(alarm_Time.this,No, intent, PendingIntent.FLAG_NO_CREATE);
if (pending_intent == null) {
Log.v("help", "PendingIntent is null");
}
else {
Log.v("help", "PendingIntent is not null");
}
//intialize alarm_off button
Button alarm_off = (Button) findViewById(R.id.alarm_off);
// create on click listner to end alarm
//final Intent finalIntent = intent;
alarm_off.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//method that change update_txt txtbox
// set_alarm_text("Alarm off!");
//cancel the alarm
alarmManager.cancel(pending_intent);
// put extra string into my_intent
// tells the clock that you pressed the "alarm off" button
intent.putExtra("extra", "alarm off");
// stop the ringtone
sendBroadcast(intent);
}
});
}}
解决方案
This is the code in your service's onStartCommand()
:
// set up an intent that goes to the Main Activity
Intent intent_main_activity = new Intent(this.getApplicationContext(),
alarm_Time.class);
intent.putExtra("extra", "alarm on");
intent.putExtra("Alarm_no", No);
You want to put the "extras" in intent_main_activity
and not in intent
.
这篇关于额外的不经过意图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!